How to reload a page using JavaScript
Asked Answered
K

13

1374

How can I reload the page using JavaScript?

I need a method that works in all browsers.

Keller answered 15/9, 2010 at 6:3 Comment(0)
I
1458

JavaScript 1.2 and newer

Short version:

location.reload();

Long version (identical to the short version):

window.location.reload();

Some browsers support an optional boolean parameter that will forcibly clear the cache (similar to Ctrl+Shift+R), but this is nonstandard and poorly supported and documented and should generally not be used:

//Force a hard reload to clear the cache if supported by the browser
window.location.reload(true);

JavaScript 1.1

window.location.replace(window.location.pathname + window.location.search + window.location.hash);
// does not create a history entry

JavaScript 1.0

window.location.href = window.location.pathname + window.location.search + window.location.hash;
// creates a history entry
Impertinence answered 15/9, 2010 at 6:17 Comment(2)
It looks like location.reload(forceReload: boolean) is deprecated now (just the true/false parameter), though it might still work.Challenging
As far as I know, it never was part of any specification. To force reloading (bypassing the cache), adding a random parameter which is not used otherwise to the URL as a dirty solution will work. Maybe something like nocache=' + (new Date()).getTime().Dorman
M
660
location.reload();

See this MDN page for more information.

If you are refreshing after an onclick then you'll need to return false directly after

location.reload();
return false;
Magenmagena answered 15/9, 2010 at 6:4 Comment(7)
what is the difference between location.reload() and window.location.reload() ?Cantor
@ShivanRaptor Usually none, in web browsers context, location is the same as window.location as window is the global object.Magenmagena
don't forget to return false; if calling this from an onclick in a link.Creak
I prefer to use window.location.reload(); for readability, as location could be a local variable - whereas you'd usually avoid variables of the name window.Intertidal
@Creak - IMHO, return false; is not strictly necessary in a handler: doesn't reload supersede any further processing?Irksome
Why would you need to return false afterwards?Conant
The question of "Why would you need to return false afterwards?" is currently open for speculation in this post: "Why you need to return false after location.reload() with onclick?"Fieldfare
F
174

I was looking for some information regarding reloads on pages retrieved with POST requests, such as after submitting a method="post" form.

To reload the page keeping the POST data, use:

window.location.reload();

To reload the page discarding the POST data (perform a GET request), use:

window.location.href = window.location.href;

Hopefully this can help others looking for the same information.

Foxworth answered 19/7, 2017 at 19:42 Comment(3)
Thanks for the answer including a reference to reloading a document retrieved with a POST request.Solanum
Good distinction between GET and POST methodsPungent
window.location.href = window.location.href does not reload the page if the current url contains a #. You could remove the hash if you don't need it window.location.href = window.location.href.split('#')[0];.Mohl
S
114

You can perform this task using window.location.reload();. As there are many ways to do this but I think it is the appropriate way to reload the same document with JavaScript. Here is the explanation

JavaScript window.location object can be used

  • to get current page address (URL)
  • to redirect the browser to another page
  • to reload the same page

window: in JavaScript represents an open window in a browser.

location: in JavaScript holds information about current URL.

The location object is like a fragment of the window object and is called up through the window.location property.

location object has three methods:

  1. assign(): used to load a new document
  2. reload(): used to reload current document
  3. replace(): used to replace current document with a new one

So here we need to use reload(), because it can help us in reloading the same document.

So use it like window.location.reload();.

Online demo on jsfiddle

To ask your browser to retrieve the page directly from the server not from the cache, you can pass a true parameter to location.reload(). This method is compatible with all major browsers, including IE, Chrome, Firefox, Safari, Opera.

Symphonic answered 23/12, 2013 at 9:48 Comment(2)
Ah ha! replace() turned out to be the solution I was looking for because I needed to reload my page with a slight change in the query string.Frans
From w3schools: "The difference between assign() and replace(), is that replace() removes the URL of the current document from the document history, meaning that it is not possible to use the "back" button to navigate back to the original document."Sherrill
T
84

Try:

window.location.reload(true);

The parameter set to 'true' reloads a fresh copy from the server. Leaving it out will serve the page from cache.

More information can be found at MSDN and in the Mozilla documentation.

Tomlin answered 8/6, 2014 at 22:5 Comment(5)
what if I want to refresh an external web page www.xyz.com/abc ?Arnitaarno
@Dev: You should try with "replace" as mentioned later onStenotype
@Orane: I like both the simplicity and resourcefulness of this answer particularly, as to give many options just to satisfy the op's inquiry, that said, thank you for providing an authority referenced link in this situation as well, for future reference as each browser have their own JS engine for rendering...I think that the question is very general and that it should depend on what do you want do that for, because else web reference: "phpied.com/files/location-location/location-location.html" might satisfy completely the op for this question. However, this one helped me ;)Stenotype
Is this still true? Both of those links don't mention any parameter for reload.Ritch
the second ( true ) parameter is deprecatedFetter
G
55

This works for me:

function refresh() {    
    setTimeout(function () {
        location.reload()
    }, 100);
}

http://jsfiddle.net/umerqureshi/znruyzop/

Gurule answered 8/1, 2015 at 7:52 Comment(4)
This, to me, is preferable, as it avoids a loop condition on the server-sideAnemic
I particularly appreciate the inclusion of the timeout function because I have a message I still want viewed before the refresh happens. Kudos!Forspent
you can set different values as a second argument to the setTimeout function but it will doesn`t work, reload will occurs immediatily, how to solve it?Alti
@Alti don't think so. lets say if you set the value to 5000ms then reload will occur after 5 seconds. see jsfiddle.net/umerqureshi/znruyzop/446Gurule
J
50

To reload a page using JavaScript, use:

window.location.reload();
Julesjuley answered 25/5, 2018 at 10:5 Comment(0)
P
19

If you put

window.location.reload(true);

at the beginning of your page with no other condition qualifying why that code runs, the page will load and then continue to reload itself until you close your browser.

Preciosity answered 1/3, 2013 at 4:7 Comment(3)
Well, or until you open another URL. It probably depends on the browser's ability to handle continuous page (re)loads.Icon
well that depends of where you place it as you have correctly stated. We are referring here merely that perhaps an anchor tag is not the intuitive element to execute an action such as reloading the page. Suggestion: We such rely on another UI object as target to do an onclick event. You can do this in jQuery after the page is loaded by targetting a div, for example if you decorate it with css.. It depends on what you want to do with it. In that context a button rather than an input type button.if we are not planning on passing an argument to backend app it seems more appropriate.Stenotype
IE: <button onclick="javascript:window.location.reload(true);"Stenotype
M
14
location.href = location.href;
Miguelmiguela answered 15/9, 2010 at 6:7 Comment(2)
Modern browsers ignore this because the href does not change so there is no need to reload it. You should use this only as failover for old browsers without reload: (location.reload ? location.reload() : location.href = location.href)Helaina
@RadekPech Which older browsers don't have location.reload()?Andyane
P
12

Shortest (more)

history.go()
Pyrotechnics answered 28/10, 2019 at 8:32 Comment(1)
How does this effect post/form data? like with 'window.location.reload(true);' (keep data) and 'window.location.href = window.location.href;' (discard data)Absurdity
T
8

This should work:

window.location.href = window.location.href.split( '#' )[0];

or

var x = window.location.href;
x = x.split( '#' );
window.location.href = x[0];

I prefer this for the following reasons:

  • Removes the part after the #, ensuring the page reloads on browsers that won't reload content that has it.
  • It doesn't ask you if want to repost last content if you recently submit a form.
  • It should work even on most recent browsers. Tested on Lasted Firefox and Chrome.

Alternatively, you may use the most recent official method for this task

window.location.reload()
Terylene answered 2/10, 2017 at 16:17 Comment(0)
A
2

you can use this code:

window.location.reload();
Ambie answered 8/5, 2023 at 5:58 Comment(0)
B
0

The Javascript reload() method is used to reload the current document or URL. The javascript location.reload(true) method work just like reload button in your browser. By default, the JS reload() method reloads the page from the cache, however you may force it to reload the page from the server side by setting the forceGet parameter to true: location. reload(true). Source: https://www.coderepublics.com/JavaScript/javascript-location-reload-true.php

What about Depricated? It is only the reload with forcedReload which is now deprecated. But to avoid depricated error you can use location.reload() without the forceReload flag.

Barbate answered 11/2, 2022 at 14:46 Comment(1)
I need a method that works in all browsers. So location.reload(true) is a bad approach AMO developer.mozilla.org/en-US/docs/Web/API/Location/reloadBiscay

© 2022 - 2024 — McMap. All rights reserved.