Skip to content

Instantly share code, notes, and snippets.

@zhannes
Created April 9, 2012 17:22
Show Gist options
  • Save zhannes/2344834 to your computer and use it in GitHub Desktop.
Save zhannes/2344834 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);
@dey-dey
Copy link

dey-dey commented Apr 30, 2012

nice loading script. DIY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment