How do I refresh a page using JavaScript?
20 Answers
Use location.reload()
.
For example, to reload whenever an element with id="something"
is clicked:
$('#something').click(function() {
location.reload();
});
The reload()
function takes an optional parameter that can be set to true
to force a reload from the server rather than the cache. The parameter defaults to false
, so by default the page may reload from the browser's cache.

- 24,552
- 19
- 101
- 135

- 43,878
- 2
- 26
- 26
-
33This didn't work for me. This worked though: window.location.href=window.location.href; – Yster Dec 08 '15 at 09:20
-
6This didn't work for me. `window.location.href=window.location.href;` and `location.href=location.href;` worked. – twharmon Oct 06 '16 at 06:40
-
29`window.location.reload(true);` will hard refresh, otherwise by default it's false – Sagar Naliyapara Apr 25 '17 at 06:39
-
1Just run `window.location.reload()`! – Jan 26 '19 at 15:00
-
The extra argument in `location.reload(true)` is not standard but most browsers support it. Some discussion and history in [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/Location/reload). "...has never been part of any specification for location.reload() ever published" – Dr. Red Feb 04 '23 at 03:52
There are multiple unlimited ways to refresh a page with JavaScript:
location.reload()
history.go(0)
location.href = location.href
location.href = location.pathname
location.replace(location.pathname)
location.reload(false)
If we needed to pull the document from the web-server again (such as where the document contents change dynamically) we would pass the argument as
true
.
You can continue the list being creative:
window.location = window.location
window.self.window.self.window.window.location = window.location
- ...and other 534 ways
var methods = [
"location.reload()",
"history.go(0)",
"location.href = location.href",
"location.href = location.pathname",
"location.replace(location.pathname)",
"location.reload(false)"
];
var $body = $("body");
for (var i = 0; i < methods.length; ++i) {
(function(cMethod) {
$body.append($("<button>", {
text: cMethod
}).on("click", function() {
eval(cMethod); // don't blame me for using eval
}));
})(methods[i]);
}
button {
background: #2ecc71;
border: 0;
color: white;
font-weight: bold;
font-family: "Monaco", monospace;
padding: 10px;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.5s ease;
margin: 2px;
}
button:hover {
background: #27ae60;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

- 109,027
- 88
- 289
- 474
-
12+1 for the list and jsfiddle. I have a question, in jsfiddle 1 and 6 make the generated page to disappear for a moment as it being reloaded, and 2-5 make page reload being "unnoticeable". In dev tool in chrome I can see the page being regenerated, but could you explain the redrawing process being defferent? please. Thank you in advance. – Cԃաԃ Jun 25 '13 at 07:21
-
@Cԃաԃ I see no difference... Maybe the cache is the issue? I will take a look soon. – Ionică Bizău Jul 09 '13 at 15:22
-
21 and 6 `(reload()/(false))` are slower. hmmm. interesting. :) and 1 and 6 are the same as `reload()`'s default parameter is `false`. – Cԃաԃ Jul 19 '13 at 00:32
-
1`location.href = location.href` is what I usually use, but thanks for the others. Very useful! +1 – Amal Murali Dec 25 '13 at 16:10
-
1@Cԃաԃ Finally I can reproduce what you see and I asked [here](http://stackoverflow.com/q/20856899/1420197). – Ionică Bizău Dec 31 '13 at 13:49
This works on all browsers:
location.reload();

- 24,552
- 19
- 101
- 135

- 6,871
- 2
- 19
- 19
-
If you're wondering how well supported this is exactly, here's the [Can I use?](https://caniuse.com/mdn-api_location_reload) – A Friend Oct 01 '20 at 16:53
Lots of ways will work, I suppose:
window.location.reload();
history.go(0);
window.location.href=window.location.href;
-
42This `window.location.href=window.location.href;` will do **nothing** if your URL has a #/hashbang on the end `example.com/something#blah`: – AaronLS Jun 12 '13 at 16:01
-
18In case anyone's wondering what the difference between `location.reload()` and `history.go(0)` is: there is none. The relevant section of the HTML 5 spec at http://www.w3.org/TR/html5/browsers.html#dom-history-go explicitly dictates that they are equivalent: *"When the `go(delta)` method is invoked, if the argument to the method was omitted or has the value zero, the user agent must act as if the `location.reload()` method was called instead."* – Mark Amery Oct 19 '15 at 20:49
-
The only one that worked for me was this one: window.location.href=window.location.href; – Yster Dec 08 '15 at 09:10
-
If you're using jQuery and you want to refresh the contents of a page asynchronously, then you can do the following:
$.ajax({
url: "",
context: document.body,
success: function(s,x){
$(this).html(s);
}
});
The approach here that I used was Ajax jQuery. I tested it on Chrome 13. Then I put the code in the handler that will trigger the reload. The URL is ""
, which means this page.
Note that this may not be what the asker meant by "refresh". Generally, in common usage, that term refers to reloading the page in the way that the browser would do if the user clicked the "Refresh/reload" button provided in the browser's user interface. However, this is not the only possible meaning of "refresh", and it may be preferred in a specific circumstance.
What this code does is get new and updated content to the page. It does this by making an asynchronous HTTP request for the new content, and then replacing the page's current content with the new asynchronously-loaded content. It has the result of refreshing the contents of the page, even though it works slightly differently.
You'll have to choose which one you actually want, and which works best for your specific use-case.

- 239,200
- 50
- 490
- 574

- 6,509
- 4
- 30
- 34
-
6One trouble with reloading the entire HTML like this is having to manually call onload/ready events and mitigate the the overwriting of previously declared variables whose state you may wish to retain after the refresh. – PassKit Mar 14 '13 at 07:41
-
3Unless you're very careful with your code this _will_ lead to memory leaks where you've attached event handlers and such without detaching them before replacing the code they're attached to. – Lucy Llewellyn Sep 06 '13 at 21:25
-
I'm using this to reload our dashboard every second, zero flicker! It's the poor man's comet/json api. Thanks to @DanielLlewellyn et al. for warnings. – William Entriken Feb 13 '14 at 17:38
-
Minor issue with this approach discussed at: https://stackoverflow.com/questions/21761931/google-hosted-libraries-is-unnecessarily-using-cache-breakers/21761975?noredirect=1#21761975 – William Entriken Feb 13 '14 at 18:03
-
The only problem that I have with this solution is that it feels like a hack; otherwise this solution is extremely useful in refreshing only portions of the page. Also, @DanielLlewellyn, advice on memory leaks is true for any usage of .html( ) - always use .off.on for event listeners. – tim-montague Jan 25 '15 at 12:18
-
This is horrifically broken! Just a few of the problems: 1) You're trying to set the entire HTML page, including the ``, `` and `` tags, as a child of the existing `` element. This is illegal, and will result in lots of stuff from your `` being duplicated in your body. 2) You won't reset any JavaScript variables that would be reset by a real refresh. 3) You'll reload (and rerun) scripts. Seriously, don't ever use this. Just try it in the console of this Stack Overflow page and observe how all the UI functionality gets totally messed up. – Mark Amery Oct 19 '15 at 21:10
-
5A few people have commented that this approach is useful to refresh only a portion of the page. It isn't. I think those people are misunderstanding the `context` parameter of [`$.ajax`](http://api.jquery.com/jquery.ajax/) and expecting it to somehow perform some kind of magic. All it does is [set the `this` value of callback functions](http://stackoverflow.com/a/5097214/1709587). Ajax to a URL of `""` will hit the URL you're currently on, thereby ordinarily loading the complete HTML (including `` and `` and ``), and nothing in this answer filters out the stuff you don't want. – Mark Amery Oct 19 '15 at 21:20
If the current page was loaded by a POST request, you may want to use
window.location = window.location.pathname;
instead of
window.location.reload();
because window.location.reload()
will prompt for confirmation if called on a page that was loaded by a POST request.

- 143,130
- 81
- 406
- 459

- 1,641
- 1
- 13
- 11
-
13This will lose the querystring however, whereas window.location = window.location will not – mrmillsy Nov 15 '13 at 15:08
-
1@mrmillsy `window.location = window.location` is also imperfect, however; it does nothing if there is a fragid (hashbang) in the current URL. – Mark Amery Oct 19 '15 at 21:30
You're spoiled for choice; any of the following work:
window.location.href = window.location.href;
window.location.reload();
history.go(0);

- 239,200
- 50
- 490
- 574

- 49,507
- 13
- 108
- 140
You may want to use
location.reload(forceGet)
forceGet
is a boolean and optional.
The default is false which reloads the page from the cache.
Set this parameter to true if you want to force the browser to get the page from the server to get rid of the cache as well.
Or just
location.reload()
if you want quick and easy with caching.

- 30,738
- 21
- 105
- 131

- 4,009
- 8
- 40
- 60
Three approaches with different cache-related behaviours:
location.reload(true)
In browsers that implement the
forcedReload
parameter oflocation.reload()
, reloads by fetching a fresh copy of the page and all of its resources (scripts, stylesheets, images, etc.). Will not serve any resources from the cache - gets fresh copies from the server without sending anyif-modified-since
orif-none-match
headers in the request.Equivalent to the user doing a "hard reload" in browsers where that's possible.
Note that passing
true
tolocation.reload()
is supported in Firefox (see MDN) and Internet Explorer (see MSDN) but is not supported universally and is not part of the W3 HTML 5 spec, nor the W3 draft HTML 5.1 spec, nor the WHATWG HTML Living Standard.In unsupporting browsers, like Google Chrome,
location.reload(true)
behaves the same aslocation.reload()
.location.reload()
orlocation.reload(false)
Reloads the page, fetching a fresh, non-cached copy of the page HTML itself, and performing RFC 7234 revalidation requests for any resources (like scripts) that the browser has cached, even if they are fresh are RFC 7234 permits the browser to serve them without revalidation.
Exactly how the browser should utilise its cache when performing a
location.reload()
call isn't specified or documented as far as I can tell; I determined the behaviour above by experimentation.This is equivalent to the user simply pressing the "refresh" button in their browser.
location = location
(or infinitely many other possible techniques that involve assigning tolocation
or to its properties)Only works if the page's URL doesn't contain a fragid/hashbang!
Reloads the page without refetching or revalidating any fresh resources from the cache. If the page's HTML itself is fresh, this will reload the page without performing any HTTP requests at all.
This is equivalent (from a caching perspective) to the user opening the page in a new tab.
However, if the page's URL contains a hash, this will have no effect.
Again, the caching behaviour here is unspecified as far as I know; I determined it by testing.
So, in summary, you want to use:
location = location
for maximum use of the cache, as long as the page doesn't have a hash in its URL, in which case this won't worklocation.reload(true)
to fetch new copies of all resources without revalidating (although it's not universally supported and will behave no differently tolocation.reload()
in some browsers, like Chrome)location.reload()
to faithfully reproduce the effect of the user clicking the 'refresh' button.

- 1
- 1

- 143,130
- 81
- 406
- 459
-
+1 for location = location; not many people seem to suggest it although it's shorter than most the answers provided, the only downside is it's readability I guess whereas location.reload() is a bit more semantic – brandito Feb 16 '18 at 05:09
-
@Brandito More importantly, `location = location` doesn't work if there's a hash in the URL. For any site that uses fragids - or even for any site where somebody linking to a page might conceivably add a fragid to the URL - that makes it broken. – Mark Amery Feb 16 '18 at 15:12
window.location.reload()
will reload from the server and will load all your data, scripts, images, etc. again.
So if you just want to refresh the HTML, the window.location = document.URL
will return much quicker and with less traffic. But it will not reload the page if there is a hash (#) in the URL.

- 30,738
- 21
- 105
- 131
-
This behaviour is true in Chrome, at least - `location.reload()` forces revalidation of cached resources, even without arguments, while `window.location = document.URL` is happy to serve cached resources without hitting the server as long as they're fresh. – Mark Amery Oct 20 '15 at 03:53
The jQuery Load
function can also perform a page refresh:
$('body').load('views/file.html', function () {
$(this).fadeIn(5000);
});

- 30,738
- 21
- 105
- 131

- 905
- 1
- 12
- 22
As the question is generic, let's try to sum up possible solutions for the answer:
Simple plain JavaScript Solution:
The easiest way is a one line solution placed in an appropriate way:
location.reload();
What many people are missing here, because they hope to get some "points" is that the reload() function itself offers a Boolean as a parameter (details: https://developer.mozilla.org/en-US/docs/Web/API/Location/reload).
The Location.reload() method reloads the resource from the current URL. Its optional unique parameter is a Boolean, which, when it is true, causes 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.
This means there are two ways:
Solution1: Force reloading the current page from the server
location.reload(true);
Solution2: Reloading from cache or server (based on browser and your config)
location.reload(false);
location.reload();
And if you want to combine it with jQuery an listening to an event, I would recommend using the ".on()" method instead of ".click" or other event wrappers, e.g. a more proper solution would be:
$('#reloadIt').on('eventXyZ', function() {
location.reload(true);
});

- 1,487
- 13
- 24
Here is a solution that asynchronously reloads a page using jQuery. It avoids the flicker caused by window.location = window.location
. This example shows a page that reloads continuously, as in a dashboard. It is battle-tested and is running on an information display TV in Times Square.
<!DOCTYPE html>
<html lang="en">
<head>
...
<meta http-equiv="refresh" content="300">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script>
function refresh() {
$.ajax({
url: "",
dataType: "text",
success: function(html) {
$('#fu').replaceWith($.parseHTML(html));
setTimeout(refresh,2000);
}
});
}
refresh();
</script>
</head>
<body>
<div id="fu">
...
</div>
</body>
</html>
Notes:
- Using
$.ajax
directly like$.get('',function(data){$(document.body).html(data)})
causes css/js files to get cache-busted, even if you usecache: true
, that's why we useparseHTML
parseHTML
will NOT find abody
tag so your whole body needs to go in an extra div, I hope this nugget of knowledge helps you one day, you can guess how we chose the id for thatdiv
- Use
http-equiv="refresh"
just in case something goes wrong with javascript/server hiccup, then the page will STILL reload without you getting a phone call - This approach probably leaks memory somehow, the
http-equiv
refresh fixes that

- 37,208
- 23
- 149
- 195
I found
window.location.href = "";
or
window.location.href = null;
also makes a page refresh.
This makes it very much easier to reload the page removing any hash. This is very nice when I am using AngularJS in the iOS simulator, so that I don't have to rerun the app.

- 30,738
- 21
- 105
- 131

- 8,654
- 8
- 40
- 61
You can use JavaScript location.reload()
method.
This method accepts a boolean parameter. true
or false
. If the parameter is true
; the page always reloaded from the server. If it is false
; which is the default or with empty parameter browser reload the page from it's cache.
With true
parameter
<button type="button" onclick="location.reload(true);">Reload page</button>
With default
/ false
parameter
<button type="button" onclick="location.reload();">Reload page</button>
Using jquery
<button id="Reloadpage">Reload page</button>
<script type="text/javascript">
$('#Reloadpage').click(function() {
location.reload();
});
</script>

- 8,437
- 8
- 49
- 80

- 1,246
- 2
- 19
- 30
If you are using jQuery and want to refresh, then try adding your jQuery in a javascript function:
I wanted to hide an iframe from a page when clicking oh an h3
, for me it worked but I wasn't able to click the item that allowed me to view the iframe
to begin with unless I refreshed the browser manually...not ideal.
I tried the following:
var hide = () => {
$("#frame").hide();//jQuery
location.reload(true);//javascript
};
Mixing plain Jane javascript with your jQuery should work.
// code where hide (where location.reload was used)function was integrated, below
iFrameInsert = () => {
var file = `Fe1FVoW0Nt4`;
$("#frame").html(`<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/${file}\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe><h3>Close Player</h3>`);
$("h3").enter code hereclick(hide);
}
// View Player
$("#id-to-be-clicked").click(iFrameInsert);

- 7,755
- 11
- 41
- 69

- 161
- 1
- 5
You can write it in two ways. 1st is the standard way of reloading the page also called as simple refresh
location.reload(); //simple refresh
And another is called the hard refresh. Here you pass the boolean expression and set it to true. This will reload the page destroying the older cache and displaying the contents from scratch.
location.reload(true);//hard refresh
You can simply use location.reload()
.
document.querySelector("button").addEventListener("mousedown", function () {
location.reload(); // reloads the page
}, false);
* {
margin: 0px;
padding: 0px;
}
button {
background-color: #90ffc9;
border: 1px solid #000000;
border-radius: 4px;
outline: none;
padding: 1%;
top: 50%;
left: 50%;
position: absolute;
transform: translate(-50%, -50%);
transition: background-color 350ms;
}
button:hover {
background-color: #80af99;
cursor: pointer;
}
<button>
Reload
</button>

- 65
- 7