163

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.

joeybab3
  • 295
  • 2
  • 7
  • 24
Stefan Đorđević
  • 1,781
  • 3
  • 15
  • 14

18 Answers18

359

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

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

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

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

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

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

The Button element - developer.mozilla.org

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 4
    what's the difference between these three options? Is one better than the others for some situations? – ashleedawg Mar 02 '19 at 11:15
  • 1st and 2nd are different ways to reload the page, but the 3rd does a "_full_" reload, meaning that if there's any text on input forms. they'll emptied. – Pedro Lobito Feb 05 '21 at 01:00
  • Tested the suggestions and noticed that it needs to be logged in to work. Is impossible to make a button that does a reload on the page even if the user is anonymous? – Christer Mar 07 '23 at 10:45
20

This works for me:

function refreshPage(){
    window.location.reload();
} 
<button type="submit" onClick="refreshPage()">Refresh Button</button>
TylerH
  • 20,799
  • 66
  • 75
  • 101
12

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">
volt
  • 963
  • 8
  • 17
10
<a onClick="window.location.reload()">Refresh</a>

This really works perfect for me.

Awais Jameel
  • 1,838
  • 19
  • 14
8

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.

ilias iliadis
  • 601
  • 8
  • 15
  • Can you please explain what do you mean by: "***Only this realy reloads page (Today)***" ? – Pedro Lobito Dec 12 '17 at 07:48
  • "Today" means the day I wrote it (because every day something changes). "Only this" is relevant to answers given above by Pedro Lobito. – ilias iliadis Dec 13 '17 at 10:29
  • 2
    All answets on SO are "***as of today***" and subject to be updated if anything changes, that's the nature of almost anything in life, not only answers. – Pedro Lobito Dec 23 '17 at 06:39
  • Still good advice today:) Had a form that was not getting updated using other page refresh methods, this one solved the issue for me! Thank you, was pulling my hair out... – Woody Mar 13 '21 at 22:28
  • Hi all, which one of the examples does a forced reload of the page, i.e. not using the browser cache? Or do they all? – David.P Oct 31 '22 at 17:24
7

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

<a href="./">Reload</a>
Smile
  • 3,832
  • 3
  • 25
  • 39
6

button that refresh the page on click

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

<button onClick="window.location.reload();">
Love Kumar
  • 1,056
  • 9
  • 10
5

just create the empty reference on link such as following

 <a href="" onclick="dummy(0);return false;" >
Hamid s k
  • 138
  • 2
  • 6
5
<button onclick=location=URL>Refresh</button>

This might look funny but it really does the trick.

Thielicious
  • 4,122
  • 2
  • 25
  • 35
4
<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">

2

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

<button onClick="window.location.reload();">
prashant kute
  • 333
  • 4
  • 9
1

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>
dankswoops
  • 103
  • 7
1
   <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>
0

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");
}
0

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>
Dev18
  • 105
  • 1
  • 1
  • 5
esotericpig
  • 282
  • 1
  • 3
  • 14
0
<button type="submit" onclick="refresh">refresh</button>

This worked for me!

-2

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!

Minh Trung
  • 29
  • 3
-2

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

function reload() {
  window.location.refresh();
}
<button onClick="reload()"></button>
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 03 '22 at 19:17