Skip to content

Instantly share code, notes, and snippets.

Created December 21, 2012 22:17
Show Gist options
  • Select an option

  • Save anonymous/4356236 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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