Created
September 20, 2010 15:13
-
-
Save cou929/588046 to your computer and use it in GitHub Desktop.
Load external scripts one by one.
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
/** | |
* script-loader.js | |
* Load external scripts one by one. | |
* (Load certain script after previous script is loaded.) | |
*/ | |
(function script_loader(scripts) { | |
if (scripts.length <= 0) return; | |
var uri = scripts.shift(); | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.charset = 'utf-8'; | |
script.src = uri; | |
script.onload = function() { script_loader(scripts); }; // call myself recursively when script is loaded | |
script.onreadystatechange = function() { // IE supports onreadystatechange instead of onload | |
if (script.readyState === 'loaded' || script.readyState === 'complete') { | |
script.onload(scripts); | |
script.onreadystatechange = script.onload = null; | |
} | |
}; | |
document.body.appendChild(script); | |
})([ | |
'http://example.com/example1.js', | |
'http://example.com/example2.js', | |
'http://example.com/example3.js' | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment