So the refresh should discard the cache, how to do it with jQuery?
Asked
Active
Viewed 3.3k times
6
-
2possible duplicate of [Clear the cache in JavaScript](http://stackoverflow.com/questions/1011605/clear-the-cache-in-javascript) – John Flatness Sep 27 '11 at 00:47
4 Answers
28
No jQuery required, pure JavaScript:
location.reload(true);
See this article for a more in depth explanation.

gilly3
- 87,962
- 25
- 144
- 176
7
The ability to modify the browser cache is outside the scope of what jQuery can do.

Greg Hewgill
- 951,095
- 183
- 1,149
- 1,285
-
-
1`location.reload(true)` only forces a reload from the server of the *page* data. It does not modify the browser cache and does not affect whether *other* documents (such as scripts, stylesheets, or images) referenced from the page are loaded from the cache or not. – Greg Hewgill Sep 27 '11 at 01:16
-
According to [this article](http://blogs.msdn.com/b/ieinternals/archive/2010/07/08/technical-information-about-conditional-http-requests-and-the-refresh-button.aspx), `location.reload(true)` behaves the same as CTRL + f5. – gilly3 Sep 27 '11 at 02:03
-
That's a good resource, at least for IE. I'm not sure I would expect all other browsers to behave consistently, so using a nonce value ensures the correct content is loaded. – Greg Hewgill Sep 27 '11 at 02:27
-
+1 since this mentions jQuery, which is what the OP specifically asked about, even if it wasn't an actual "answer" per se. I would suggest gilly3's answer above is as good as it gets from a javascript perspective. – Carnix Jan 19 '15 at 14:33
-
Wait a moment, what about `jQuery.globalEval( "location.reload(true);" );` – lisandro Mar 13 '21 at 13:48
4
This won't be possible.
Just increment the version number to your javascript reference like this every time you wish to force a fresh download:
<script language="javascript" src="scripts/script.js?ver=2"></script>
Just to clarify, the appended ver
parameter has no intrinsic value on the script src
attribute as Slomojo pointed out. It was merely a way to address the problem of a forced file reload and also keep a neat versioning system.
-
-1 - This reads as if "version number" is some sort of intrinsic parameter of a script src uri. Which of course it isn't. In addition, the version number of the script.js isn't necessarily being incremented, you just want to force the server to throw you a fresh copy, so name your parameter something more meaningful, e.g. `reload=RANDOM`, `flush=RANDOM` etc. This way you are communicating more about the underlying principle, ie. That the browser and server see a new uri and thus avoid the cache. – ocodo Sep 27 '11 at 01:14
-
1No it doesn't. `ver` can just be something you have named, it could be anything, like `reload`. The end result is the same, the script will be freshly downloaded. Anyway my answer is inappropriate on the grounds of wanting to refresh from JavaScript. – GONeale Sep 27 '11 at 01:16
-
What it infers is the important thing, hopefully you realise I'm not suggeting that *you* think it's an intrinsic parameter, I'm simply pointing out that a novice may well get that impression. – ocodo Sep 27 '11 at 01:18
-
I am also merely saying `ver` is a good practice to use alongside the the new file being served, as why are you wanting a fresh download? Chances are the file has been modified, therefore it truly is a new revision, or version. – GONeale Sep 27 '11 at 01:22
-
Yes perhaps a novice might think it has some magical value in the script src uri. But I see it a common practice, anyway I might delete the post as it's really irrelevant to reloading via JavaScript. – GONeale Sep 27 '11 at 01:24
-
1You could just have easily added `?rand=243234`. This in combination with @gilly3's answer might work. – Mottie Sep 27 '11 at 01:32
-
Yep totally, just thought it would be more meaningful to use a version numbering system as well. But sure, rand, whatever, anything works. (Yay I'm brought into the positive again, lol, thanks fudgey) – GONeale Sep 27 '11 at 01:34
-
+1 for the update. I think leaving this here is helpful. If this wasn't such a novice question I think it wouldn't make any difference having `ver` since you could assume more prior knowledge, but searches for this are likely to bring beginners. – ocodo Sep 27 '11 at 01:36
-
-1 This doesn't actually address the OP's original question. If I understand it correctly, this question is aimed at the page cache and is looking for a way to simulate the CTRL+F5 browser behavior, NOT how to make sure a specific resource isn't cached. These two things are very different. – Carnix Jan 19 '15 at 14:30
-
Yes, but I'm reading between the lines.... most likely, he is suffering from particular .JS and/or .CSS files caching. I'm pretty sure even location.reload(true) doesn't refresh these from the server properly, it refetches the "page" from the server yes, but doesn't serve you a non-cached copy of some of the resources, I'm pretty sure.. – GONeale Jan 22 '15 at 00:28
-
Oh, I could be wrong, https://developer.zendesk.com/blog/static-file-cache-busting seems to indicate it bypasses cache and pulls down any resource files from the server too. – GONeale Jan 22 '15 at 00:32
-4
You can detect ctrl+F5 using following code
$(document).keydown(function(e) {
if (e.keyCode == 116 && e.ctrlKey) {
alert('ctrl + F5');//HEHEEE
}
});
-
1While certainly correct syntactically, this answer is unrelated to the question. – Carnix Jan 19 '15 at 14:34