Skip to content

Instantly share code, notes, and snippets.

@jerrybendy
Created July 9, 2019 07:40
Show Gist options
  • Save jerrybendy/7f98a1c2e63220529a3d6efdbab24454 to your computer and use it in GitHub Desktop.
Save jerrybendy/7f98a1c2e63220529a3d6efdbab24454 to your computer and use it in GitHub Desktop.
Load a JavaScript file from the server, then execute it.
/**
* Load a JavaScript file from the server, then execute it.
*
* Code comes from `https://github.com/ded/script.js`
* If your want more features, please follow the original
*
* @usage:
* loadScript('http://example.com/test.js', function() {
* console.log('Loaded')
* });
*/
function loadScript(jsFile, callback) {
var el = document.createElement('script'), loaded
el.onload = el.onerror = el.onreadystatechange = function() {
if ((el.readyState && !(/^c|loade/.test(el.readyState))) || loaded) return;
el.onload = el.onreadystatechange = null
loaded = 1
callback()
}
el.async = true
el.src = jsFile;
document.head.appendChild(el)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment