Created
December 21, 2012 22:17
-
-
Save anonymous/4356236 to your computer and use it in GitHub Desktop.
getScript and leave a trace!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to find the author on StackExchange somewhere