Create Page Reload Button Using JavaScript


Hello Friends, In this tutorial we will learn to create a page reload button using JavaScript by adding an event listener to a button element that triggers the location.reload() method when clicked. Here is an example code:

HTML:

<button id="reloadButton">Reload Page</button>

JavaScript:

const reloadButton = document.getElementById("reloadButton");
reloadButton.addEventListener("click", () => {
  location.reload();
});

In this code, we first select the button element using the document.getElementById() method and store it in a variable called reloadButton. Then, we add an event listener to the reloadButton that listens for a "click" event. When the button is clicked, the event listener triggers an anonymous arrow function that calls the location.reload() method, which reloads the current page.

       

Advertisements

ads