We can create a button that refreshes the page on click with JavaScript by listening to the click event that the button triggers.
For instance, we can write the following HTML:
<input type="button" value="Reload Page">
Then we can add a click listener to it by writing:
const input = document.querySelector('input')
input.addEventListener('click', () => {
window.location.reload();
})
We get the input with document.querySelector
.
Then we call addEventListener
with 'click'
to add a click listener.
The callback runs when the button is clicked.
And we call window.location.reload
to reload the page.
We can replace window.location.reload
with history.go(0)
, which will also reload the page.
To do that, we write:
const input = document.querySelector('input')
input.addEventListener('click', () => {
history.go(0);
})
to do the reloading.