3

When loading content through AJAX which contains an externally linked Javascript file or when using jQuery.getScript() function call, the linked Javascript files are appended with a cache busting parameter, which prevents the file from being cached by the browser.

So, instead of writ­ing some­thing like <script src="/js/foo.js">, it writes some­thing like <script src="/js/foo.js?_=ts2477874287">, caus­ing the script to be loaded fresh each time.

Is there a way to disable this and have the file cached when it's loaded?

1 Answer 1

3

I don't know about getScript, but cache is a parameter you can set in a .ajax() parameter map. It's false by default for scripts but you can flip it to true. Once false, it won't append a cache-busting query string.

[updated per comment]

Sign up to request clarification or add additional context in comments.

4 Comments

I think you have the true/false in reverse. Default for cache is true, false for dataType 'script' and 'jsonp'. Thus flipping it to true, it should avoid the cache busting parameter? -- They don't seem to have a way to do this in getScript, and unfortunately it still doesn't help me figure out how to override generic <script> tags (i.e. not loaded through an ajax call), so the only thing I can currently think of is that I'd have to use jQuery.ajax with cache set to true for all dynamically loaded content with linked external JS files.
Yes, I had it straight in my head but wrote it down flipped. Thanks! I'm failing to understand what you mean about generic script tags, but that's probably on me.
Naw, it's on me. I wasn't clear. Assume I'm using jquery.load('mypage.html'); The mypage.html contains HTML as well as a linked script: <script src="./js/foo.js"> yet even that gets the cache buster parameter.
Oh, now that's interesting. Alas I don't know anything about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.