Created
August 3, 2017 14:40
-
-
Save mmilosheski/a7eb0edc9ad1887e1e1318cda46f9159 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function injectScript(src) { | |
return new Promise((resolve, reject) => { | |
const script = document.createElement('script'); | |
script.async = true; | |
script.src = src; | |
script.addEventListener('load', resolve); | |
script.addEventListener('error', () => reject('Error loading script.')); | |
script.addEventListener('abort', () => reject('Script loading aborted.')); | |
document.head.appendChild(script); | |
}); | |
} | |
injectScript('http://example.com/script.js') | |
.then(() => { | |
console.log('Script loaded!'); | |
}).catch(error => { | |
console.log(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment