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.).
-
I strongly recommend accepting a different answer. See the comments and the note I added at the top. – Inigo Dec 16 '22 at 09:37
-
No fault of this question, but it was 13yrs ago, and many of the solutions proposed don't work across all platforms. – Ben in CA Mar 15 '23 at 16:31
-
I added a bounty, but still not getting any updated relevant answers. Is there really nothing in web standards to achieve such a "simple" task? – Ben in CA Mar 22 '23 at 19:30
10 Answers
⚠️ This solution won't work on all browsers. MDN page for
location.reload()
:Note: Firefox supports a non-standard forceGet boolean parameter for location.reload(), to tell Firefox to bypass its cache and force-reload the current document. However, in all other browsers, any parameter you specify in a location.reload() call will be ignored and have no effect of any kind.
Try:
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:

- 12,186
- 5
- 41
- 70

- 807,428
- 183
- 922
- 838
-
26I 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
-
-
7Did 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
-
1yes, 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
-
9It 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
-
1
-
3I 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
-
11
-
1For 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
-
5location.reload() has no parameter: https://developer.mozilla.org/en-US/docs/Web/API/Location/reload – gear Dec 09 '21 at 08:00
window.location.href = window.location.href

- 759
- 1
- 10
- 19
-
26This will not pull the page from the server if the browser has it cached. – LukeP Apr 07 '20 at 20:42
-
4Please add some explanation to your answer such that others can learn from it – Nico Haase Jan 20 '21 at 16:58
-
Works for my case! I think it cleared POST data in a WordPress plugin I'm working on. – Marina Dunst Jul 09 '21 at 15:55
This is a 2022 update with 2 methods, considering SPA's with #
in url:
METHOD 1:
As mentioned in other answers one solution would be to put a random parameter to query string. In javascript it could be achieved with this:
function urlWithRndQueryParam(url, paramName) {
const ulrArr = url.split('#');
const urlQry = ulrArr[0].split('?');
const usp = new URLSearchParams(urlQry[1] || '');
usp.set(paramName || '_z', `${Date.now()}`);
urlQry[1] = usp.toString();
ulrArr[0] = urlQry.join('?');
return ulrArr.join('#');
}
function handleHardReload(url) {
window.location.href = urlWithRndQueryParam(url);
// This is to ensure reload with url's having '#'
window.location.reload();
}
handleHardReload(window.location.href);
The bad part is that it changes the current url and sometimes, in clean url's, it could seem little bit ugly for users.
METHOD 2:
Taking the idea from https://splunktool.com/force-a-reload-of-page-in-chrome-using-javascript-no-cache, the process could be to get the url without cache first and then reload the page:
async function handleHardReload(url) {
await fetch(url, {
headers: {
Pragma: 'no-cache',
Expires: '-1',
'Cache-Control': 'no-cache',
},
});
window.location.href = url;
// This is to ensure reload with url's having '#'
window.location.reload();
}
handleHardReload(window.location.href);
Could be even combined with method 1, but I think that with headers should be enought:
async function handleHardReload(url) {
const newUrl = urlWithRndQueryParam(url);
await fetch(newUrl, {
headers: {
Pragma: 'no-cache',
Expires: '-1',
'Cache-Control': 'no-cache',
},
});
window.location.href = url;
// This is to ensure reload with url's having '#'
window.location.reload();
}
handleHardReload(window.location.href);

- 8,339
- 11
- 59
- 82
-
Hey Miquel. This is probably a really stupid question, but what exact url and paramName am I passing to this function? Is it just my current url? Thanks! Is the url just windoiw.location.href, or do I have to split it or adjust it at all? – Powermaster Prime Sep 28 '22 at 12:47
-
url is the url you want to update query parameter, and paramname is the parameter you want to update. For instance, if you call `urlWithRndQueryParam('https://google.com/whatever?_z=1234/#/start', '_z')` you'll get `'https://google.com/whatever?_z=83234/#/start'`. Just try it in stackblitz – Miquel Sep 28 '22 at 13:53
-
Miquel, sadly it seems like these methods won't hard refresh the CSS like the actual Google commands behave. Any advice on some possible solutions? Perhaps I'm implementing your methods incorrectly. – Powermaster Prime Oct 10 '22 at 16:48
-
1mmm @OldsDiesel this is quite strange, precisely `pragma` and `cache-control` are the headers that controls cache behavior in both sides (client and server). Could you check if you see in devtools the background request with these headers and which is the result? If the result is the non cached one, it is working fine, it might be a problem of reloading. Otherwise it could be that the fetch request is not being sent. Please note that you should wait `handleHardReload` to finish: `await handleHardReload(window.location.href)` – Miquel Oct 10 '22 at 17:05
-
1For css you could try to dynamically load css scripts with a script in header that adds the random time parameter, the idea could be to have an array with the scripts you want to load and inject them directly with `document.createElement` and `document.body.appendChild` – Miquel Oct 10 '22 at 17:07
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.

- 1,492
- 1
- 23
- 53
-
1i am also having hard time looking for a cache busting solution for my react app and on front end nothing seems to work , do you find any solution yet apart from modifying resources from the server ? – Mohd Maaz Jul 17 '22 at 06:50
-
These are usual cases for server side caching, like for example cloudflare caching. Where scripts and assets are being cached on their end. In such cases only you need above method. – Lost Stranger Mar 22 '23 at 07:52
-
1No fault of the poster, but this isn't ideal, and I was hoping in 2023 there's a "simple" way of forcing a refresh - but I didn't get any better answers when I posted a bounty, so awarding it to you. – Ben in CA Mar 22 '23 at 19:38
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.

- 5,970
- 3
- 36
- 36
-
-
Sounds like you're running it on script load instead of invoking it within a function. At least that shows it works. – ShortFuse Sep 05 '22 at 20:26
-
It need an if. Se my answer: https://stackoverflow.com/a/73683077/12690348 – Dan Froberg Sep 11 '22 at 22:44
-
You answer has `refresh()` being called on script load. That's why it's doing an endless loop. You coded it to refresh on load. – ShortFuse Sep 14 '22 at 16:56
-
Thank you, I append this to my answer: It do no endless loop because of the if-statement described. The refresh() function can be called by a button or other conditioned ways instead of page load. – Dan Froberg Sep 18 '22 at 08:28
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: ElementRef<HTMLFormElement>;
forceReload() {
this.refreshForm.nativeElement.submit();
}
}
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.
Not using Angular
Apparently, "Fetching the file by post does clear the cached file and it seems to work in any browser"
So using "he fetch api with POST" should also work

- 6,334
- 6
- 41
- 78
-
Thank you! Thank you! I have tried all the other client side solutions and none worked to hard refresh a .wasm file! But your did (I did not use angular, I used the fetch api with POST)! Fetching the file by post does clear the cached file and it seems to work in any browser. :) – RandomGuy Aug 25 '23 at 10:19
-
1You're welcome ;) I've updated my answer to add the information you gave me – Raphaël Balet Aug 25 '23 at 12:08
UPDATED to refresh all the external resources (images, JavaScript, CSS, etc.)
Put this in file named HardRefresh.js
:
function hardRefresh() {
const t = parseInt(Date.now() / 10000); //10s tics
const x = localStorage.getItem("t");
localStorage.setItem("t", t);
if (x != t) location.reload(true) //force page refresh from server
else { //refreshed from server within 10s
const a = document.querySelectorAll("a, link, script, img")
var n = a.length
while(n--) {
var tag = a[n]
var url = new URL(tag.href || tag.src);
url.searchParams.set('r', t.toString());
tag.href = url.toString(); //a, link, ...
tag.src = tag.href; //rerun script, refresh img
}
}
}
window.addEventListener("DOMContentLoaded", hardRefresh);
window.addEventListener("deviceorientation", hardRefresh, true);
This code do a fully controled forced hard refresh for every visitor, so that any update will show up without a cashing problem.
Duplicated DOM rendering is not a performance issue, because the first render is from cache and it stops rendering in <script src="js/HardRefresh.js">
where it reload a page from server. When it run a refreshed page it also refresh urls in page.
The last refresh time x
is stored in localStorage. It is compared with the current time t
to refresh within 10 seconds. Assuming a load from server not take more than 10 sec we manage to stop a page refresh loop, so do not have it less than 10s.
For a visitor of page the x != t
is true since long time ago or first visit; that will get page from server. Then diff is less than 10s and x == t
, that will make the else
part add query strings to href
and src
having sources to refresh.
The refresh() function can be called by a button or other conditioned ways. Full control is managed by refining exclusion and inclusion of urls in your code.

- 168
- 8
-
While this does produce "mypage.com/page?r=166821181", my page still uses cached images. Is there something else I can do? – David.P Nov 12 '22 at 00:10
-
1@David.P Code is now updated by set href with new query string and it will then reload. Client updates also .js and .css files! – Dan Froberg Dec 11 '22 at 04:55
-
-
Try simplify! Force page refresh from server seem not to be needed. Remove the if. Change document.querySelectorAll("a, link, script, img") to document.querySelectorAll("a"). For css, js and resources you can use a minimal dynamic loader with query. – Dan Froberg Apr 04 '23 at 09:45
The most reliable way I've found is to use a chache buster by adding a value to the querystring.
Here's a generic routine that I use:
function reloadUrl() {
// cache busting: Reliable but modifies URL
var queryParams = new URLSearchParams(window.location.search);
queryParams.set("lr", new Date().getTime());
var query = queryParams.toString();
window.location.search = query; // navigates
}
Calling this will produce something like this:
https://somesite.com/page?lr=1665958485293
after a reload.
This works to force reload every time, but the caveat is that the URL changes. In most applications this won't matter, but if the server relies on specific parameters this can cause potential side effects.

- 17,302
- 14
- 89
- 134
-
While this does produce "https://mypage.com/page?lr=1665958485293", my page still uses cached images. What to do now? – David.P Nov 11 '22 at 23:34
-
-
1@David.P ... that's because the images are their own resources with their own URLs. You'd have to parse all the images in the document, and also add randomizers to their URLs as well. Something akin to Dan Frodberg's answer above. – eidylon Apr 11 '23 at 18:36
-
Thanks. Some time ago I found an easy JavaScript method that hard reloads all images after the page has loaded. I shall post that method here as soon as I get around to look it up. – David.P Apr 12 '23 at 21:43
The accepted answer location.reload(true);
DOES NOT WORK. Here's why (taken directly from the MDN page):
Note: Firefox supports a non-standard forceGet boolean parameter for location.reload(), to tell Firefox to bypass its cache and force-reload the current document. However, in all other browsers, any parameter you specify in a location.reload() call will be ignored and have no effect of any kind.
You may, though, come across instances of location.reload(true) in existing code that was written with the assumption the force-reload effect occurs in all browsers. A GitHub "location.reload(true)" search returns several hundred thousand results. So there's a lot of existing code which has it.
The history of it is: some version of Netscape Navigator added support for it, which apparently eventually got picked up in Firefox. And at one point the W3C Web APIs Working Group took up an issue to consider adding it to the specification for location.reload(). However, it was never actually added.
So a boolean parameter is not part of the current specification for location.reload() — and in fact has never been part of any specification for location.reload() ever published.
Source: https://developer.mozilla.org/en-US/docs/Web/API/Location/reload
So, how to force a hard refresh? Two ideas:
Use query strings, as suggested/explained/demoed in multiple other answers in this thread;
Flood the W3C with requests.
https://www.w3.org/Consortium/contact
(Had suggestion #2 been widely adopted years ago, we could have stopped using the query string hack years ago.)

- 37,875
- 18
- 96
- 111
-
Does the query string method "flush" any cached JS content of a React SPA? Unless the file is named differently by webpack I suppose. – Ben in CA Mar 22 '23 at 19:28
The location.reload(true)
is deprecated so there is no direct option to hard reload but we can still manually clear the cache and then reload.
Use this custom function instead.
export const clearCache = (reloadAfterClear = true) => {
if('caches' in window){
caches.keys().then((names) => {
names.forEach(async (name) => {
await caches.delete(name)
})
})
if(reloadAfterClear)
window.location.reload()
}
}
If you don't want to refresh immidiately and just clear the cache, use clearCache(false)
Hope it helps as none of the above options worked for me.

- 248
- 2
- 6