Skip to content

Instantly share code, notes, and snippets.

@jasondmoss
Last active November 17, 2018 06:12
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 jasondmoss/4cd4b843edb283cf8fd0 to your computer and use it in GitHub Desktop.
Save jasondmoss/4cd4b843edb283cf8fd0 to your computer and use it in GitHub Desktop.
jQuery: Load external scripts and cache them.
/**
* Cache AJAX-included scripts.
*/
$.cachedScript = function (url, options) {
options = $.extend(
options || {}, {
dataType: "script",
cache: true,
url: url
}
);
return $.ajax(options);
};
/* ------------------- */
// Usage.
var myScript = siteUrl +"/js/my-script.js";
$.cachedScript(myScript).done(
function (script, textStatus) {
console.log(myScript +": "+ textStatus);
}
);
/* <> */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment