Home » Programming » Javascript » Javascript Refresh Page

How to Refresh the Page in JavaScript using location.reload(), With Examples

Here’s a short guide to refreshing a webpage with JavaScript, with examples.

There are over 500 ways to trigger a page reload using JavaScript. All but one are unofficial or are the side effect of another behavior.

Here’s how to do it properly.

Using location.reload()

Simply call:

location.reload()

…anywhere in your code to trigger a page reload. It’s that easy.

What about window.location.reload() ?

You may see some use:

window.location.reload()

This is exactly the same as using location.reload() – the window object in JavaScript is the global context, so there’s usually no need to specify it explicitly when running location.reload().

If for some reason, you have another variable called location and there is a conflict, you can specify window.location.reload() to get around this.

See more about scopes in JavaScript here.

Reloading from onclick Attribute or Event

You can call for a reload from an HTML elements onclick attribute directly:

<a href="#" onclick="location.reload()">Reload</a>

More Information

The location.reload() function doesn’t accept any parameters (it may have in the past in some browsers but is no longer officially supported).

Find out more about it here:

https://developer.mozilla.org/en-US/docs/Web/API/Location/reload

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment