Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferred method to reload page with JavaScript? [closed]

which way to reload a current page (using a button) would you prefer?

1 <input type="button" value="Reload" onClick="history.go(0)"> 2 <input type="button" value="Reload" onClick="location.reload(true)"> 3 <input type="button" value="Reload" onClick="window.location.reload(true)"> 4 <input type="button" value="Reload" onClick="window.location.href=window.location.href"> 5 <input type="button" value="Reload" onClick="document.location.reload(true)"> 6 <input type="button" value="Reload" onClick="document.location.href=document.location.href"> 

As the URL of the page changes frequently AFAIK a 'fallback function' like

<a href="urlOfCurrentPage.html" onclick="window.location.reload(true);return false;">Reload</a> 

won't work for me, right?

like image 983
Mel Avatar asked Apr 12 '10 17:04

Mel


People also ask

Which method is used to refresh the webpage in JavaScript?

JavaScript reload() method In JavaScript, the reload() method is used to reload a webpage. It is similar to the refresh button of the browser.

How do I reload a page in JavaScript?

In JavaScript, you refresh the page using document. location. reload() . You can add the true keyword to force the reloaded page to come from the server (instead of cache).

Which of the following JavaScript functions is used for reloading the current page or document?

The reload() method reloads the current document. The reload() method does the same as the reload button in your browser.


2 Answers

Depends on what you want to do. The fourth and sixth methods there won't reload any form data, they essentially make a separate visit to the page. Some versions of Firefox also have issues with the third method. Other than that, I'd go with the fifth as a personal preference. It seems the clearest.

like image 120
tloflin Avatar answered Nov 24 '22 07:11

tloflin


You may also do:

wd represents window || document:

  • wd.location.assign(wd.location.href) : go to the URL
  • wd.location.replace(wd.location.href) : go to the URL and replace previous page in history
  • wd.location.reload(<true/false/blank>) : reload page from server/cache/cache
like image 24
vol7ron Avatar answered Nov 24 '22 09:11

vol7ron