I use getScript to load dynamically my plugin:
$.getScript('js/code.photoswipe.jquery-3.0.4.min.js', function () {
//do magic
});
How do I disable caching busting? At the moment it generates numbers at the end: js/code.photoswipe.jquery-3.0.4.min.js?_=1326992601415 I saw this, but not sure how to use it in my case:
$.getScript = function (url, callback, cache) { $.ajax({ type: "GET", url: url, success: callback, dataType: "script", cache: cache }); };If I call $.getScript multiple times adding the same js file, does it do request each time to get that file? If so, is there a way to check if we already imported that script, so we could avoid calling getScript again for the same file?
-
If I call $.getScript multiple times adding the same js file, does it do request each time to get that file? Without caching, separate requests are sent because of cache busting. With caching, it will be loaded from browser cache (browser might double check with server to see if it has the latest copy depending on how strong the caching headers are).Salman Arshad– Salman Arshad2015年10月23日 23:11:52 +00:00Commented Oct 23, 2015 at 23:11
5 Answers 5
How to disable the cache busting:
$.ajaxSetup({
cache: true
});
That will ensure users grab the script from the server only once and from their local cache thereafter (unless their browser settings prevent caching).
1 Comment
The jQuery docs for $.getScript say that getScript is shorthand for:
$.ajax({
url: url,
dataType: "script",
success: success
});
http://api.jquery.com/jQuery.getScript/
This means that you just need to add a cache : true parameter to that.
$.ajax({
url: url,
cache : true,
dataType: "script",
success: success
});
Simple as that. The getScript function is nothing special, just shorthand.
Comments
The short answer is unfortunately, you can't.
getScript can however be overridden like this:
$.getScript = function(url, callback, cache){
$.ajax({
type: "GET",
url: url,
success: callback,
dataType: "script",
cache: true
});
};
If you include this in your js somewhere, it won't break existing code, and will also cache the fetched script.
Advantage
Using above snippet, you can enable caching for all the scripts on your page. So you don't need to re-specify with each script fetch.
1 Comment
$.getScript so you can $.getScript({cache:true,url:url}) without overriding anything.1 . jQuery 1.12.0 and later supports the options object signature:
$.getScript({
url: "foo.js",
cache: true
})
2 . Assuming you set cache:true the file will be loaded from the browser cache (unless browser cache is disabled or expired, for example when devtools is open)
1 Comment
Your browser will cache the url accordingly already.. So you don't have to worry about caching.
If however you want to bust the cache simply add a random string to the url like so:
$.getScript('js/code.photoswipe.jquery-3.0.4.min.js?' + Math.random(), function () {
//do magic
});
?' + Math.random() will allow for a random number to append to your js file so caching is broken with each request for the file (as it randomly generates a number)