Created
March 15, 2014 17:47
-
-
Save james2doyle/9571121 to your computer and use it in GitHub Desktop.
loadScript. Async load a script and then fire a callback when loaded.
This file contains hidden or 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
//this function will work cross-browser for loading scripts asynchronously | |
function loadScript(src, callback) { | |
var s, | |
r, | |
t; | |
r = false; | |
s = document.createElement('script'); | |
s.type = 'text/javascript'; | |
s.src = src; | |
s.onload = s.onreadystatechange = function() { | |
//console.log( this.readyState ); //uncomment this line to see which ready states are called. | |
if (!r && (!this.readyState || this.readyState == 'complete')) { | |
r = true; | |
callback(); | |
} | |
}; | |
t = document.getElementsByTagName('script')[0]; | |
t.parent.insertBefore(s, t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment