Created
July 9, 2019 07:40
-
-
Save jerrybendy/7f98a1c2e63220529a3d6efdbab24454 to your computer and use it in GitHub Desktop.
Load a JavaScript file from the server, then execute it.
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
/** | |
* 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