Web development · 30 Aug 2014 · 3 min read
Redirect to a new page after a few seconds with JavaScript
A delayed redirect sends visitors to another page after a few seconds, for example after a form is submitted (“Thanks, taking you back to the home page…”) or from an old page that has moved. Here are the ways to do it, and when not to.
setTimeout and window.location
Wait three seconds (3,000 milliseconds), then go to the new address:
<script>
setTimeout(function () {
window.location.href = "https://example.com/new-page/";
}, 3000);
</script>
The old page stays in the browser history, so the Back button returns to it.
Without a Back-button loop: location.replace()
If the old page only exists to send people on, pressing Back would land them on it again and start the timer again. location.replace() swaps the current history entry instead of adding one:
setTimeout(() => {
window.location.replace("https://example.com/new-page/");
}, 3000);
Without JavaScript: meta refresh
A <meta> tag in the page’s <head> does the same thing and works even when JavaScript is off:
<meta http-equiv="refresh" content="3; url=https://example.com/new-page/">
The number is the delay in seconds. Use 0 for an immediate redirect.
Show a countdown and a link
Telling people what is about to happen, and letting them go now, is friendlier than a silent jump:
<p>
This page has moved. Taking you to the
<a id="redirect-link" href="https://example.com/new-page/">new page</a>
in <span id="countdown">5</span> seconds.
</p>
<script>
const link = document.getElementById("redirect-link");
const counter = document.getElementById("countdown");
let seconds = 5;
const timer = setInterval(() => {
seconds -= 1;
counter.textContent = seconds;
if (seconds <= 0) {
clearInterval(timer);
window.location.replace(link.href);
}
}, 1000);
</script>
The URL lives in one place (the link), so the script and the fallback can’t drift apart.
Accessibility
A page that moves on by itself can catch out people who read slowly or use a screen reader. The Web Content Accessibility Guidelines (WCAG 2.2, criterion 2.2.1 “Timing adjustable”) ask that people can turn off, adjust or extend a time limit. The simplest way to respect that: always show a visible link to the destination, keep the delay reasonable, and don’t put a delayed redirect on a page with content people need to read.
When to use a server redirect instead
If a page has moved permanently, a JavaScript or meta-refresh redirect is the wrong tool. Use a server-side 301 redirect:
- It is instant; nobody waits on a page that no longer matters.
- Search engines move the old page’s ranking to the new URL reliably.
- It works for every client, including ones that don’t run JavaScript.
How you set one up depends on the host: a Redirect 301 line in Apache’s .htaccess, a return 301 in Nginx, a _redirects file on Cloudflare Pages or Netlify, or a redirect plugin in WordPress. Keep delayed client-side redirects for the cases where people need to see a message first.
Quick reference
| Situation | Use |
|---|---|
| Show a message, then move on | setTimeout + location.href |
| The old page should not stay in history | location.replace() |
| No JavaScript, or a simple static page | <meta http-equiv="refresh"> |
| The page has moved for good | A server-side 301 |