Categories
JavaScript Answers

How to refresh the page on click of a button with JavaScript?

Spread the love

Sometimes, we want to refresh the page on click of a button with JavaScript.

In this article, we’ll look at how to refresh the page on click of a button with JavaScript.

How to refresh the page on click of a button with JavaScript?

To refresh the page on click of a button with JavaScript, we can call the window.location.reload method in the button click event handler.

For instance, we write:

<button type="button">Close</button>

to add a button.

Then we write:

const button = document.querySelector('button')
button.onclick = () => {
  window.location.reload();
}

to select a button with document.querySelector('button').

Then we set the button.onclick property to the click handler function.

In it, we call window.location.reload to reload the page.

Conclusion

To refresh the page on click of a button with JavaScript, we can call the window.location.reload method in the button click event handler.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *