Reload the Page with JavaScript: location.reload()

Call location.reload() to reload the current page, exactly as the browser's reload button does. It takes no standard arguments. The old reload(true) form, meant to skip the cache, is a non-standard parameter that MDN lists as supported only in Firefox, so other browsers ignore the true. history.go(0) also reloads.

Ways to refresh or move the page
CodeWhat happensAdds a history entry?
location.reload()Reloads the current URLNo
history.go(0) or history.go()Same result as location.reload()No
location.replace(url)Loads url and replaces the current entryNo, Back skips the old page
location.assign(url)Loads url as a new stepYes
<meta http-equiv="refresh" content="30">Reloads after 30 seconds, no script neededNo

Updated

Reloading is one line of code, but the reasons people reach for it vary: picking up new data, resetting a stuck state, refreshing a dashboard on a timer or forcing a new version of the site. This guide shows the standard call, what the older variations really do, and how to avoid a reload entirely when fetching fresh data is all you need.

Reload code you can paste

A reload button

The click handler calls the standard reload with no argument.

<button type="button" id="reload">Refresh</button>
<script>
  document.getElementById("reload")
    .addEventListener("click", () => location.reload());
</script>

Refresh on a timer, but only while the tab is visible

Hidden tabs are skipped, and a tab that comes back after the interval reloads straight away.

const EVERY_MS = 60_000;
let last = Date.now();

setInterval(() => {
  if (document.visibilityState === "visible" && Date.now() - last >= EVERY_MS) {
    location.reload();
  }
}, 5_000);

document.addEventListener("visibilitychange", () => {
  if (document.visibilityState === "visible" && Date.now() - last >= EVERY_MS) {
    location.reload();
  }
});

Reload exactly once

The sessionStorage flag survives the reload in the same tab, so the second load skips it.

if (!sessionStorage.getItem("reloadedOnce")) {
  sessionStorage.setItem("reloadedOnce", "1");
  location.reload();
}

Warn before reloading with unsaved changes

Browsers only show the prompt after the person has interacted with the page, and they use their own wording.

let dirty = false;
form.addEventListener("input", () => { dirty = true; });

window.addEventListener("beforeunload", (e) => {
  if (dirty) e.preventDefault();
});

Reload versus fetching fresh data

A full reload throws away the whole page: the DOM, every variable, open menus, typed input and usually the scroll position. That is the right reset when the page is in a bad state or a new version must load.

When you only need new numbers on a dashboard, call fetch, update the part of the page that changed and leave the rest. It is quicker, uses less data and does not flash the screen.

Why a reload may still show old files

A normal reload lets the browser revalidate cached files with the server, but files served with a long cache lifetime can still come straight from the cache. Code that tries to force a hard refresh from JavaScript cannot fix that across browsers.

The dependable fix lives on the server and in the build: put a content hash in script and style file names so every deploy gets new URLs, and serve the HTML with a short or no-cache lifetime so it always points at the latest files.

How it works

  1. Pick a refresh method and an interval in the generator.
  2. The code box updates with a ready to paste snippet for that method.
  3. Use the buttons to try location.reload() and history.go(0) on this page. Both reload it.
  4. Read the cache section before relying on a reload to show newly deployed files.

Tips

Frequently asked questions

Does location.reload(true) clear the cache?

Not reliably. The forceGet argument is not part of the HTML standard, and MDN notes only Firefox supports it. Other browsers ignore it and do a normal reload. To make sure people get new files, version your asset URLs or set proper Cache-Control headers.

What is the difference between location.reload() and location.href = location.href?

reload() always reloads the current document. Assigning href starts a navigation to that URL, and when the URL contains a #fragment the browser only scrolls to the fragment, so nothing reloads. Use reload() when a reload is what you want.

How do I refresh a page every 30 seconds?

Without script, add <meta http-equiv="refresh" content="30"> to the head. With script, setTimeout(() => location.reload(), 30000). Use auto refresh sparingly: MDN warns that unexpected refreshes can disorient people and conflict with WCAG guidance on timing.

Can a script reload a page from another site?

No. Calling reload on another origin's Location throws a SecurityError DOMException, so a script inside an iframe from a different site cannot reload the parent, and the parent cannot reload it that way.

How do I reload the page once only?

Mark that you already reloaded, for example with a sessionStorage flag or a query parameter such as ?r=1, and check it before calling reload. Without a guard, code that reloads on load creates an endless loop.

Related: JavaScript localStorage guide, Fetch data without reloading, JavaScript event listeners, Scroll to an element, HTML boilerplate.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap