Created
December 18, 2018 08:35
-
-
Save timneutkens/cd714f3a1d5f8238692644ac61d4deae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function importScript(src) { | |
return new Promise((resolve, reject) => { | |
const loaded = Boolean(document.querySelector(`script[src="${src}"]`)) | |
if (loaded) return resolve() | |
const script = document.createElement('script') | |
script.type = 'text/javascript' | |
script.src = src | |
script.async = true | |
script.onload = () => resolve() | |
script.onerror = reject | |
const head = document.getElementsByTagName('head')[0] | |
head.appendChild(script) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment