Created
June 5, 2013 06:18
-
-
Save TexRx/5711959 to your computer and use it in GitHub Desktop.
pure js async script loader
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
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; | |
if(callback) { | |
callback(); | |
} | |
} | |
}; | |
t = document.getElementsByTagName('head')[0]; | |
t.appendChild(s); | |
} | |
/* Usage */ | |
(function() { | |
loadScript('the/path/to/your/script.js'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment