Resh
Resh

Reputation: 14079

How to reload a page using JavaScript

How can I reload the page using JavaScript?

I need a method that works in all browsers.

Upvotes: 1407

Views: 2055909

Answers (13)

gianebao
gianebao

Reputation: 18958

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

Upvotes: 1515

OmidDarvishi
OmidDarvishi

Reputation: 650

you can use this code:

window.location.reload();

Upvotes: 3

Kuldeep Bisht
Kuldeep Bisht

Reputation: 43

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.

Upvotes: 0

starwarswii
starwarswii

Reputation: 2417

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.

Upvotes: 185

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92627

Shortest (more)

history.go()

Upvotes: 12

Lekensteyn
Lekensteyn

Reputation: 66465

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;

Upvotes: 662

Umer Qureshi
Umer Qureshi

Reputation: 1766

This works for me:

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

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

Upvotes: 55

Girish Ninama
Girish Ninama

Reputation: 593

To reload a page using JavaScript, use:

window.location.reload();

Upvotes: 51

Orane
Orane

Reputation: 2261

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.

Upvotes: 84

Nikhil Agrawal
Nikhil Agrawal

Reputation: 26538

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.

Upvotes: 115

Ayoola Falola
Ayoola Falola

Reputation: 81

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()

Upvotes: 8

guilin 桂林
guilin 桂林

Reputation: 17422

location.href = location.href;

Upvotes: 14

Joe Devlin
Joe Devlin

Reputation: 207

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.

Upvotes: 19

Related Questions