JavaScript Redirect Code
Generate a JavaScript redirect using either location.replace or location.href, and understand which of the two you actually want.
How to use it
- Enter the destination — and the old path where the method needs one.
- Choose 301 for a permanent move or 302 for a temporary one.
- Copy the generated code into the right place for your server or page.
A JavaScript redirect changes window.location from client-side script, which is useful when the destination depends on something only the browser knows — a condition checked after page load, a value read from localStorage, a redirect fired from inside an event handler. It runs after the page has already loaded and parsed, so it is always slower than a server-side redirect and should not be the first choice for a permanent page move that search engines need to understand.
location.href vs location.replace — the difference that matters
window.location.href = url navigates to the new page and adds a history entry, so pressing back returns to the redirect page — which immediately fires again, trapping the visitor in a forward-back loop. window.location.replace(url) overwrites the current history entry instead of adding a new one, so back skips straight past the redirect page to wherever the visitor was before it.
replace() is almost always what you actually want for a redirect. Reach for href only in the rare case where you deliberately want the redirect page to remain in the browser history.
Questions people ask
What is the difference between href and replace?
href adds a history entry, so the back button returns to the old page and redirects again — a loop. replace overwrites the entry, which is almost always what you want.
Is anything uploaded?
No. Everything runs in your browser — no file, image or snippet you use here ever leaves your device.
Last updated 22 August 2026