Skip to content

Instantly share code, notes, and snippets.

Created December 21, 2012 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4356236 to your computer and use it in GitHub Desktop.
Save anonymous/4356236 to your computer and use it in GitHub Desktop.
getScript and leave a trace!
// how jQuery getScript should work!
function getScript (path, callback) {
if (!getScript.loaded) {
getScript.loaded = {};
$('script').each(function () {
var result = $.Deferred();
getScript.loaded[$(this).attr('src')] = result.promise();
result.resolve();
});
}
if (!getScript.loaded[path]) {
var result = $.Deferred();
var script = document.createElement("script");
script.async = "async";
script.type = "text/javascript";
script.src = path;
script.onload = script.onreadystatechange = function(_, isAbort) {
if (!script.readyState || /loaded|complete/.test(script.readyState)) {
if (!isAbort) {
result.reject();
} else {
result.resolve();
}
}
};
script.onerror = function () {
result.reject();
};
$("head")[0].appendChild(script);
getScript.loaded[path] = result.promise();
}
if (callback) {
getScript.loaded[path].done(callback);
}
return getScript.loaded[path];
}
@robcolburn
Copy link

Need to find the author on StackExchange somewhere

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment