203

How can I force the web browser to do a hard refresh of the page via JavaScript?
Hard refresh means getting a fresh copy of the page AND refresh all the external resources (images, JavaScript, CSS, etc.).

Axel
  • 3,108
  • 10
  • 30
  • 49
leepowers
  • 35,484
  • 22
  • 93
  • 127

3 Answers3

344

Try to use:

location.reload(true);

When this method receives a true value as argument, it will cause the page to always be reloaded from the server. If it is false or not specified, the browser may reload the page from its cache.

More info:

Christian C. Salvadó
  • 723,813
  • 173
  • 899
  • 828
  • 23
    I am pretty sure this won't reload all external resources. You would have to read through all the `a`, `link`, `script` and `img` elements and append a random query string to the end of each external reference *after* the hard reload. Or, do that on the server. – Doug Neiner Jan 20 '10 at 05:39
  • @CMS Is this completely cross-browser compatible? – Marco Prins Jul 30 '14 at 13:21
  • 6
    Did it work in 2010 ? It sure doesn't work in 2018 (in Chrome). Chrome loads everything (except /Home/Index) from cache. It appears to be working in firefox WTH ? – Maciej Szpakowski Feb 23 '18 at 15:30
  • 1
    @MaciejSzpakowski Using [Cache.keys()](https://developer.mozilla.org/en-US/docs/Web/API/Cache/keys) and [Cache.delete()](https://developer.mozilla.org/en-US/docs/Web/API/Cache/delete) worked for me. Example: [jsfiddle](https://jsfiddle.net/yp47mx2o/6/) – r.delic Mar 17 '18 at 00:02
  • 1
    yes, it's correct, location.reload(true); will work for all modern browsers – Aleksandr Golovatyi May 15 '18 at 08:43
  • But whenever we force reload , there comes an alert asking "are you sure you want to reload?, is there a way to overide that alert and reload the page? – Gina Gina Jul 24 '18 at 05:21
  • this doesn't seem to work on chrome but this `window.location.href=window.location.href` worked for me – Gsp Ivan Jul 16 '19 at 11:08
  • 6
    It doesn't work for me. This doesn't clear the data that I clear using ctrl F5 – ozimax06 Oct 08 '19 at 14:12
  • 4
    I think this functionality is removed in HTML5. – Mygod Apr 03 '20 at 17:24
  • 1
    As on 16-12-20 this code is not run. Try in React => 16.8 – Hardik Desai Dec 16 '20 at 11:18
  • 2
    I don't think that this works properly. The given documentation doesn't even list a parameter to that function – Nico Haase Jan 20 '21 at 16:57
  • 2
    it deprecated and ignored by browsers nowadays. – Kurkov Igor Feb 17 '21 at 19:05
  • For those of you in the future like me. This does work in some browsers, but not in others. I'm looking at you chrome >:( – Samuel Thompson May 12 '21 at 21:42
8
window.location.href = window.location.href
Gsp Ivan
  • 572
  • 1
  • 8
  • 16
0

I was working on django , but nothing was working so this bypass worked for me , it's forwarding to the next so whenever you just want to come back it's Hard refreshing the page ! it's maybe not the proper way but can solve the issue , try it out and let me know !

window.history.forward(1);
  • This code does not reload/refresh the current page. It is just forwarding it to the next location in the history, which may or may not be present. Moreover, the OP has specified "hard refresh", which means the page should get pulled from the server again, i.e. bypassing the cache. – brc-dd May 08 '21 at 18:34
  • okk , Thnaks for letting me know, none of the above solution worked for me but this worked so , i thought it may help ... i request u to remove downvotes sir ! – Saikat Mukherjee May 08 '21 at 20:25