236

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,189
  • 11
  • 35
  • 56
leepowers
  • 36,760
  • 23
  • 97
  • 128

5 Answers5

379

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ó
  • 769,263
  • 179
  • 909
  • 832
  • 26
    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
  • 7
    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 – Ivan Jul 16 '19 at 11:08
  • 9
    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
  • 7
    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
  • 8
    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
  • 3
    location.reload() has no parameter: https://developer.mozilla.org/en-US/docs/Web/API/Location/reload – gear Dec 09 '21 at 08:00
11
window.location.href = window.location.href
Ivan
  • 639
  • 1
  • 9
  • 17
2

Accepted answer above no longer does anything except just a normal reloading on mostly new version of web browsers today. I've tried on my recently updated Chrome all those, including location.reload(true), location.href = location.href, and <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />. None of them worked.

My solution is by using server-side capability to append non-repeating query string to all included source files reference as like below example.

<script src="script.js?t=<?=time();?>"></script>

So you also need to control it dynamically when to keep previous file and when to update it. The only issue is when files inclusion is performed via script by plugins you have no control to modify it. Don't worry about source files flooding. When older file is unlinked it will be automatically garbage collected.

stack underflow
  • 1,094
  • 1
  • 17
  • 44
2

Changing the current URL with a search parameter will cause browsers to pass that same parameter to the server, which in other words, forces a refresh.

(No guarantees if you use intercept with a Service Worker though.)

  const url = new URL(window.location.href);
  url.searchParams.set('reloadTime', Date.now().toString());
  window.location.href = url.toString();

If you want support older browsers:

if ('URL' in window) {
  const url = new URL(window.location.href);
  url.searchParams.set('reloadTime', Date.now().toString());
  window.location.href = url.toString();
} else {
  window.location.href = window.location.origin 
    + window.location.pathname 
    + window.location.search 
    + (window.location.search ? '&' : '?')
    + 'reloadTime='
    + Date.now().toString()
    + window.location.hash;
}

That said, forcing all your CSS and JS to refresh is a bit more laborious. You would want to do the same process of adding a searchParam for all the src attributes in <script> and href in <link>. That said it won't unload the current JS, but would work fine for CSS.

document.querySelectorAll('link').forEach((link) => link.href = addTimestamp(link.href));

I won't bother with a JS sample since it'll likely just cause problems.

You can save this hassle by adding a timestamp as a search param in your JS and CSS links when compiling the HTML.

ShortFuse
  • 5,131
  • 3
  • 32
  • 33
1

For angular users and as found here, you can do the following:

<form [action]="myAppURL" method="POST" #refreshForm></form>
import { Component, OnInit, ViewChild } from '@angular/core';

@Component({
  // ...
})
export class FooComponent {
  @ViewChild('refreshForm', { static: false }) refreshForm;

  forceReload() {
    this.refreshForm.nativeElement.submit();
  }
}

The reason why it worked was explained on this website: https://www.xspdf.com/resolution/52192666.html

You'll also find how the hard reload works for every framework and more in this article

explanation: Angular

Location: reload(), The Location.reload() method reloads the current URL, like the Refresh button. Using only location.reload(); is not a solution if you want to perform a force-reload (as done with e.g. Ctrl + F5) in order to reload all resources from the server and not from the browser cache. The solution to this issue is, to execute a POST request to the current location as this always makes the browser to reload everything.

Raphaël Balet
  • 3,574
  • 21
  • 44