Skip to content

Instantly share code, notes, and snippets.

@markhepburn
Created November 2, 2011 22:44
Show Gist options
  • Save markhepburn/1335182 to your computer and use it in GitHub Desktop.
Save markhepburn/1335182 to your computer and use it in GitHub Desktop.
Yet another lazy script-loader, with callbacks managed using jQuery Deferred objects.
var $LL = (function($) {
var deferreds = {};
return function(s, c) {
if (s in deferreds) {
deferreds[s].done(function(){
c();
});
return;
}
deferreds[s] = $.Deferred();
var d = deferreds[s].done(function() {
c();
});
var f = function () {
if (!(this.readyState
&& this.readyState !== "complete"
&& this.readyState !== "loaded")) {
this.onload = this.onreadystatechange = null;
d.resolve();
}
},
g = document.getElementsByTagName("head")[0],
i = function (h) {
var a = document.createElement("script");
a.async = true;
a.src = h;
a.onload = a.onreadystatechange = f;
g.appendChild(a);
};
i(s);
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment