Skip to content

Instantly share code, notes, and snippets.

@jlav1n
Created October 31, 2013 18:25
Show Gist options
  • Save jlav1n/7254502 to your computer and use it in GitHub Desktop.
Save jlav1n/7254502 to your computer and use it in GitHub Desktop.
Javascript loader for asynchronous and non-blocking loading of JS. Compare this to https://github.com/ded/script.js
(function() {
function getScript(url,success){
var script=document.createElement('script');
script.src=url;
var head=document.getElementsByTagName('head')[0], done=false;
script.onload=script.onreadystatechange = function(){
if ( !done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') ) {
done=true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
getScript('http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js',function(){
// YOUR CODE GOES HERE AND IS EXECUTED AFTER JQUERY LOADS
});
})();
// dependencies:
getScript('http://myurltojquery.js',function(){
getScript('http://myurltojqueryUI.js',function(){
// your code here
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment