Skip to content

Instantly share code, notes, and snippets.

@dey-dey
Forked from zhannes/gist:2344834
Created April 30, 2012 16:20
Show Gist options
  • Save dey-dey/2559720 to your computer and use it in GitHub Desktop.
Save dey-dey/2559720 to your computer and use it in GitHub Desktop.
load a script
var loadScript = function(url,callback,async){
if(!url){ return false; }
var script = document.createElement('script'),
head = document.getElementsByTagName('head')[0];
script.src = url;
script.async = async ? true : false;
/* bind a callback for all browsers */
script.onload = script.onreadystatechange = function() {
if ( (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") ) {
callback && callback.call(window);
script.onload = script.onreadystatechange = null;
}
};
head.appendChild( script );
}
var myCallback = function(){
// code that depends on the script
};
// run it
loadScript('js/foo.js', myCallback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment