Skip to content

Instantly share code, notes, and snippets.

@cou929
Created September 20, 2010 15:13
Show Gist options
  • Save cou929/588046 to your computer and use it in GitHub Desktop.
Save cou929/588046 to your computer and use it in GitHub Desktop.
Load external scripts one by one.
/**
* 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