Button that refreshes the page on click

JavascriptButtonRefreshReload

Javascript Problem Overview


I need a button that will refresh the page on the user's click. I tried this:

<input type="button" value="Reload Page" onClick="reload">

or

<input type="button" value="Refresh Page" onClick="refresh">

But neither worked.

Javascript Solutions


Solution 1 - Javascript

Use onClick with window.location.reload(), i.e. :

<button onClick="window.location.reload();">Refresh Page</button>

Or history.go(0), i.e.:

<button onClick="history.go(0);">Refresh Page</button>

Or window.location.href=window.location.href for 'full' reload, i.e.:

<button onClick="window.location.href=window.location.href">Refresh Page</button>

The Button element - developer.mozilla.org

Solution 2 - Javascript

This works for me:

function refreshPage(){
    window.location.reload();
} 

<button type="submit" onClick="refreshPage()">Refresh Button</button>

Solution 3 - Javascript

I noticed that all the answers here use inline onClick handlers. It's generally recommended to keep HTML and Javascript separate.

Here's an answer that adds a click event listener directly to the button. When it's clicked, it calls location.reload which causes the page to reload with the current URL.

const refreshButton = document.querySelector('.refresh-button');

const refreshPage = () => {
  location.reload();
}

refreshButton.addEventListener('click', refreshPage)

.demo-image {
  display: block;
}

<button class="refresh-button">Refresh!</button>
<img class="demo-image" src="https://picsum.photos/200/300">

Solution 4 - Javascript

<a onClick="window.location.reload()">Refresh</a>

This really works perfect for me.

Solution 5 - Javascript

Only this realy reloads page (Today)

<input type="button" value="Refresh Page" onClick="location.href=location.href">

Others do not exactly reload. They keep values inside text boxes.

Solution 6 - Javascript

just create the empty reference on link such as following

 <a href="" onclick="dummy(0);return false;" >

Solution 7 - Javascript

<button onclick=location=URL>Refresh</button>

This might look funny but it really does the trick.

Solution 8 - Javascript

button that refresh the page on click

Use onClick button with window.location.reload():

<button onClick="window.location.reload();">

Solution 9 - Javascript

<button onClick="window.location.reload();">Reload</button>

Or you can also use

<form action="<same page url>" method="GET">
<button>Reload</button>
</form>

Note: Change "<same page link>" with the url of same page you want to reload. for example: <form action="home.html> method="GET">

Solution 10 - Javascript

Though the question is for button, but if anyone wants to refresh the page using <a>, you can simply do

<a href="./">Reload</a>

Solution 11 - Javascript

Use this its work fine for me to reload the same page:

<button onClick="window.location.reload();">

Solution 12 - Javascript

   <script>
    function locationreload() {
        location.reload();
          
    }
   </script>

    <button type="submit" onclick="return confirm('Do you really want to refresh your page?') , locationreload();" class="btn btn-success">Refresh</button>

Solution 13 - Javascript

If you are looking for a form reset:

<input type="reset" value="Reset Form Values"/>

or to reset other aspects of the form not handled by the browser

<input type="reset" onclick="doFormReset();" value="Reset Form Values"/>

Using jQuery

function doFormReset(){
    $(".invalid").removeClass("invalid");
}

Solution 14 - Javascript

I didn't want to use JavaScript, so I figured out a way using a "hidden form." Here's an example:

<form method="get" accept-charset="utf-8">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">

  <input type="submit" value="Read">
  <input type="submit" value="Reload" form="hidden_form">
</form>

<form id="hidden_form" method="get"></form>

Solution 15 - Javascript

<button type="submit" onclick="refresh">refresh</button>

This worked for me!

Solution 16 - Javascript

When designing a website, it's always important to consider the best practices for Content Security Policy and disabled JavaScript users.

-Inline scripts like onClick are a vulnerability.

-Ignoring the <noscript> tag and styling your elements hidden then using JS to change the display: back to its respective styling forces the user to enable JS.

This is the simplest solution for all situations.

<a href="/">Reload Page</a>

Solution 17 - Javascript

"reload" and "refresh" aren't JavaScript functions. Try using the following code:

function reload() {
  window.location.refresh();
}

<button onClick="reload()"></button>

Solution 18 - Javascript

Don't need Js, you can put button tag in a form tag:

<form>	
  <button class="restart-btn">Restart</button>
</form>

Test it and give me your opinion!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStefan ĐorđevićView Question on Stackoverflow
Solution 1 - JavascriptPedro LobitoView Answer on Stackoverflow
Solution 2 - JavascriptManjunath AkalawadiView Answer on Stackoverflow
Solution 3 - JavascriptvoltView Answer on Stackoverflow
Solution 4 - JavascriptAwais JameelView Answer on Stackoverflow
Solution 5 - Javascriptilias iliadisView Answer on Stackoverflow
Solution 6 - JavascriptHamid s kView Answer on Stackoverflow
Solution 7 - JavascriptThieliciousView Answer on Stackoverflow
Solution 8 - JavascriptLove KumarView Answer on Stackoverflow
Solution 9 - JavascriptKrishna SaxenaView Answer on Stackoverflow
Solution 10 - JavascriptSmileView Answer on Stackoverflow
Solution 11 - Javascriptprashant kuteView Answer on Stackoverflow
Solution 12 - Javascriptuser18377368View Answer on Stackoverflow
Solution 13 - JavascriptRichard Taylor-KennyView Answer on Stackoverflow
Solution 14 - JavascriptesotericpigView Answer on Stackoverflow
Solution 15 - JavascriptSajin SAView Answer on Stackoverflow
Solution 16 - JavascriptdankswoopsView Answer on Stackoverflow
Solution 17 - JavascriptChristian SamuView Answer on Stackoverflow
Solution 18 - JavascriptMinh TrungView Answer on Stackoverflow