Vanilla JavaScript: How to Reload a Page With the Window Reload Event

2 years ago

To reload a webpage with JavaScript we need to use the Window Reload Event.

Reload page with a single line of JavaScript:

window.location.reload();
Paste this code into your browser Console and hit enter.

Cool, but perhaps not so practical? Let’s try something else.

Reload page with a button click event:

<button onclick="reloadPage()">Reload page</button>
Add this to your HTML markup.

The window reload event function:


function reloadPage() {
    window.location.reload();
}   
Add this to your JavaScript file.

See it in action by clicking this button:

What are you waiting for?! Click on it!

All the code on CodePen.

Note: the window reload event doesn’t work inside the CodePen editor, but you can test it out in CodePen’s debug mode.

Resources


Has this been helpful?
Let me know in the comments!

Or share it with your friends :-)


No comments to display.