The jQuery.getScript()
method is a shorthand of the Ajax function (with the dataType attribute: $.ajax({ url: url,
dataType: "script"
})
)
If you want the scripts to be cachable, either use RequireJS or follow jQuery's example on extending the jQuery.getScript
method similar to the following.
jQuery.cachedScript = function( url, options ) {
// Allow user to set any option except for dataType, cache, and url
options = $.extend( options || {}, {
dataType: "script",
cache: true,
url: url
});
// Use $.ajax() since it is more flexible than $.getScript
// Return the jqXHR object so we can chain callbacks
return jQuery.ajax( options );
};
// Usage
$.cachedScript( "ajax/test.js" ).done(function( script, textStatus ) {
console.log( textStatus );
});
Reference: jQuery.getScript() | jQuery API Documentation