Skip to content

Instantly share code, notes, and snippets.

@JimBobSquarePants
Last active December 12, 2015 01:19
Show Gist options
  • Save JimBobSquarePants/4690594 to your computer and use it in GitHub Desktop.
Save JimBobSquarePants/4690594 to your computer and use it in GitHub Desktop.
jQuery plugins to get multiple scripts in a single call. In cached and un-cached flavours.
(function ($) {
/**
* Multiple parallel getScript with caching.
*
* @param {Array|String} url (one or more URLs)
* @param callback fn (oncomplete, optional)
* @returns {function}
*/
$.getCachedScript = function (url, callback) {
// Normalise type
if (!$.isArray(url)) {
url = [url];
}
$.when.apply(null, $.map(url, function (val) {
$.ajax({
dataType: "script",
cache: true,
url: val
});
})).done(function () {
if (callback && typeof callback === "function") {
callback();
}
});
};
}(jQuery));
(function ($) {
// Save the original version.
var getScript = $.getScript;
/**
* Multiple parallel getScript.
*
* @param {Array|String} url (one or more URLs)
* @param callback fn (oncomplete, optional)
* @returns {function}
*/
$.getScript = function (url, callback) {
// Normalise type
if (!$.isArray(url)) {
url = [url];
}
$.when.apply(null, $.map(url, getScript)).done(function () {
if (callback && typeof callback === "function") {
callback();
}
});
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment