Skip to content

Instantly share code, notes, and snippets.

@braiam
Forked from bradvin/jQuery.loadScript
Last active December 14, 2015 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braiam/5068552 to your computer and use it in GitHub Desktop.
Save braiam/5068552 to your computer and use it in GitHub Desktop.
jQuery.loadScript = function (url, arg1, arg2) {
"use strict";
var cache = false,
callback = null,
load = true;
//arg1 and arg2 can be interchangable
if ($.isFunction(arg1)) {
callback = arg1;
cache = arg2 || cache;
} else {
cache = arg1 || cache;
callback = arg2 || callback;
}
//check all existing script tags in the page for the url
jQuery('script[type="text/javascript"]').each(function () {
load = (url !== $(this).attr('src'));
});
if (load) {
//didn't find it in the page, so load it
jQuery.ajax({
type : 'GET',
url : url,
success : callback,
dataType : 'script',
cache : cache
});
} else {
//already loaded so just call the callback
if (jQuery.isFunction(callback)) {
callback.call(this);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment