Skip to content

Instantly share code, notes, and snippets.

@timneutkens
Created December 18, 2018 08:35
Show Gist options
  • Save timneutkens/cd714f3a1d5f8238692644ac61d4deae to your computer and use it in GitHub Desktop.
Save timneutkens/cd714f3a1d5f8238692644ac61d4deae to your computer and use it in GitHub Desktop.
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