Created
February 7, 2015 06:18
-
-
Save ltchronus/2f2c0f8469a3f42f1233 to your computer and use it in GitHub Desktop.
load scripts asynchronously
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
//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.parentNode.insertBefore(s, t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment