-
-
Save dey-dey/2559720 to your computer and use it in GitHub Desktop.
load a script
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
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