Skip to content

Instantly share code, notes, and snippets.

@arestov
Created December 31, 2011 07:22
Show Gist options
  • Save arestov/1543229 to your computer and use it in GitHub Desktop.
Save arestov/1543229 to your computer and use it in GitHub Desktop.
non blocking js load
(function(){
function isFileReady ( readyState ) {
// Check to see if any of the ways a file can be ready are available as properties on the file's element
return ( ! readyState || readyState == 'loaded' || readyState == 'complete' );
}
var p = document.getElementsByTagName('script'),
p = p[p.length-1];
window.loadJS = function(src, callback){
var s = document.createElement('script'),
done;
s.onreadystatechange = s.onload = function () {
if ( ! done && isFileReady( s.readyState ) ) {
// Set done to prevent this function from being called twice.
done = true;
callback();
// Handle memory leak in IE
s.onload = s.onreadystatechange = null;
}
};
s.src = src;
p.parentNode.insertBefore(s, p);
};
})();
(function(){
var cbs = [];
var jqLoaded = function(){
while (cbs.length){
cbs.pop()();
}
};
window.jqready = function(cb){
if (window.jQuery){
cb();
} else {
cbs.push(cb);
}
};
loadJS('http://yandex.st/jquery/1.7.1/jquery.min.js', function(){
if (!window.jQuery){
loadJS('https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', function(){
if (!window.jQuery){
loadJS('/js/jquery.js', function(){
if (window.jQuery){
jqLoaded();
} else {
console.log("ugly world, can't load jquery")
}
})
} else {
jqLoaded();
}
})
} else {
jqLoaded();
}
})
})();
jqready(function(){
$(function(){
$(document.body).append('<p>crazy shi</p>')
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment