How do you redirect to another page?
By FrontendPro Editorial Team Updated 9/26/2026
Answer
Assign to window.location, or call replace() — the difference is what happens to the back button.
window.location.href = "/dashboard"; // adds a history entry
window.location.replace("/dashboard"); // replaces the current entry
window.location.assign("/dashboard"); // same as setting .href
Which to use
Use href for a normal navigation the user might want to undo. Use replace when going back would be wrong — after a login redirect, or after a successful form submission, where pressing back would return the user to a page that no longer makes sense or resubmit the form.
Related navigation
history.back(); // like the browser's back button
location.reload(); // reload the current page
window.open("/help", "_blank"); // a new tab (may be blocked outside a click)
Do not use JavaScript when HTML will do
A plain link is better in every measurable way — it works before hydration, supports middle-click and "open in new tab", and search engines follow it:
<a href="/dashboard">Dashboard</a>
In a framework, use its own navigation (<Link>, router.push()) rather than window.location. Setting location triggers a full page load and throws away the client-side state the router is managing.
For SEO, redirect on the server
A JavaScript redirect happens only after the page loads and scripts run. Crawlers may not follow it, and no ranking signal is transferred. If a URL has permanently moved, send a 301/308 from the server or your framework's redirect config.
One security note: never build a redirect target from unvalidated user input such as ?next=. An open redirect sends users to an attacker's site from a URL that looks like yours. Check the value against an allowlist, or accept only paths beginning with a single /.
