Meta Refresh and JavaScript Redirect Guide
Contents
Server-side redirects (301/302) are always the right first choice. But sometimes you can't touch the server config, or you need to redirect based on runtime conditions โ that's where client-side redirects come in.
HTML meta refresh
<!-- Redirect immediately -->
<meta http-equiv="refresh" content="0; url=/new-page.html">
<!-- Redirect after 3 seconds -->
<meta http-equiv="refresh" content="3; url=https://newdomain.com">
- โ Works without JavaScript
- โ One line of HTML
- โ Can't be cancelled once the page loads
- โ Creates a back-button trap (page redirects again on back)
- โ Poor SEO โ Google discourages it
โ ๏ธ Back-button trap
Meta refresh adds the current page to browser history before redirecting. When users hit Back, they land on the redirecting page again and get sent forward immediately. Avoid this pattern whenever possible.
JavaScript redirect methods
window.location.href (most common)
// Adds current page to history โ user can go back
window.location.href = 'https://example.com';
location.replace() (no history entry)
// Replaces current history entry โ user cannot go back
location.replace('https://example.com');
location.assign() (same as href)
location.assign('https://example.com');
window.open() (new tab)
window.open('https://example.com', '_blank');
Key differences
History entry
- Adds history entry:
location.href,location.assign(), meta refresh - No history entry:
location.replace()
Cancellable
- Can be cancelled: JavaScript (use
clearTimeout) - Cannot be cancelled: meta refresh
When to use each
Use meta refresh when
- You have a static HTML page with no JavaScript
- You need a "page has moved" notice with a countdown
Use location.href when
- You need conditional redirect logic
- You want the user to be able to go back
if (!user.isLoggedIn) {
location.href = '/login';
}
Use location.replace() when
- After login โ don't let users back to the login page
- Error page auto-redirect
- Mobile/desktop detection redirect
if (/Android|iPhone/i.test(navigator.userAgent)) {
location.replace('/mobile/');
}
SEO impact
- Meta refresh โ Google officially discourages it; may cause ranking loss
- JavaScript redirects โ Google can follow them, but processing is slower than server-side redirects
- Neither carries HTTP status codes โ search engines can't tell if the move is permanent or temporary
๐จ Important
For any permanent URL change, always use a server-side 301 redirect. Client-side redirects are for dynamic, runtime scenarios only.
Common pitfalls
Code after redirect still executes
// โ Code below still runs
if (needRedirect) { location.href = '/other'; }
doSomethingElse(); // still executes!
// โ
Use return
if (needRedirect) { location.href = '/other'; return; }
doSomethingElse();
Mobile redirect loop
// โ
Guard against re-redirecting
if (isMobile && !location.pathname.startsWith('/mobile/')) {
location.replace('/mobile' + location.pathname);
}
Lost query string
// โ Query string dropped
location.href = '/new-page';
// โ
Preserve it
location.href = '/new-page' + location.search;