Computer >> Computer tutorials >  >> Programming >> Javascript

How to Reload a Page With the Window Reload Event in JavaScript

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

Reload page with a single line of JavaScript:

Paste this code into your browser Console and hit enter

window.location.reload()

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

Reload page with a button click event:

Add this to your HTML markup:

<button onclick="reloadPage()">Reload page</button>

Add he window reload event function this to your JavaScript file

function reloadPage() {
    window.location.reload()
}   

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