Question
How can I redirect a user from one page to another using jQuery or plain JavaScript?
Short Answer
By the end of this page, you will understand how browser redirects work in JavaScript, how to send a user to a new URL, when to use window.location.href versus window.location.replace(), and why jQuery is usually not needed for redirects.
Concept
Redirecting means telling the browser to load a different URL.
In web development, this is commonly done after an action such as:
- submitting a form
- logging in successfully
- logging out
- clicking a button
- checking whether a user has permission to view a page
In JavaScript, redirects are usually handled through the browser's window.location object. This object represents the current page URL and gives you ways to change it.
The most common redirect approaches are:
window.location.href = "https://example.com"— sends the user to a new pagewindow.location.assign("https://example.com")— also sends the user to a new pagewindow.location.replace("https://example.com")— sends the user to a new page without keeping the current page in browser history
This matters because browser history affects the Back button:
- with
hreforassign(), the user can usually click Back to return - with
replace(), the current page is replaced, so Back usually will not return to it
Although the question mentions jQuery, jQuery does not provide a special redirect feature. In practice, developers use normal JavaScript even inside jQuery event handlers.
Example:
$("#goButton").on("click", function () {
window.location.href = "/dashboard";
});
So the core concept is not really a jQuery feature. It is a browser navigation feature provided by JavaScript.
Mental Model
Think of window.location as the browser's address bar.
- Reading it is like looking at the current address.
- Changing it is like typing a new address and pressing Enter.
- Using
replace()is like swapping the current history entry instead of adding a new one.
So if you write:
window.location.href = "/home";
you are effectively telling the browser: "Go to this address now."
Syntax and Examples
Basic redirect syntax
window.location.href = "https://example.com";
This loads the new page and keeps the current page in browser history.
Using assign()
window.location.assign("https://example.com");
This behaves similarly to href.
Using replace()
window.location.replace("https://example.com");
This redirects without leaving the current page in the browser history.
Redirect to another page on the same site
window.location.href = "/profile";
This is common for internal navigation.
Redirect after a button click
Step by Step Execution
Consider this example:
const isLoggedIn = true;
if (isLoggedIn) {
window.location.href = "/dashboard";
}
Step by step:
isLoggedInis created and set totrue.- The
ifstatement checks whetherisLoggedInis truthy. - Because the value is
true, the code inside the block runs. window.location.hrefis assigned the value"/dashboard".- The browser starts navigating to
/dashboard. - The current page stops being the active page once navigation completes.
Now compare this version:
const isLoggedIn = true;
if (isLoggedIn) {
window.location.replace("/dashboard");
}
The early steps are the same, but the history behavior changes:
Real World Use Cases
Redirects are used in many practical situations:
- Login flow: after successful authentication, send the user to
/dashboard - Logout flow: after clearing session data, send the user to
/login - Protected pages: if a user is not authorized, redirect to
/unauthorizedor/login - Form submission: after saving data, redirect to a confirmation page
- Moved content: send users from an old route to a new route
- Language or region handling: redirect users to
/en,/fr, or region-specific pages - Checkout flow: move users to payment, confirmation, or order summary pages
Example of redirecting unauthenticated users:
if (!userIsAuthenticated) {
window.location.replace("/login");
}
This is common because you may not want users to go Back to a protected page.
Real Codebase Usage
In real projects, redirects are often combined with application logic rather than written by themselves.
Guard clauses
A common pattern is checking a condition early and redirecting immediately.
if (!token) {
window.location.replace("/login");
return;
}
This avoids deeply nested code.
After validation or success
if (formIsValid) {
saveData();
window.location.href = "/success";
}
Error handling flows
if (response.status === 401) {
window.location.replace("/login");
}
Redirect after async work
fetch("/api/logout", { method: "POST" })
.then( {
.. = ;
});
Common Mistakes
1. Thinking jQuery has a special redirect method
jQuery does not have a built-in redirect API.
// Not a real jQuery redirect method
$.redirect("/home");
Use standard JavaScript instead:
window.location.href = "/home";
2. Forgetting the difference between href and replace()
If you want the user to be able to go back, use href or assign().
If you do not want that, use replace().
window.location.replace("/login");
3. Using an invalid or incomplete URL accidentally
Broken:
window.location.href = "example.com";
Comparisons
| Approach | Example | Keeps current page in history? | Common use |
|---|---|---|---|
window.location.href = url | window.location.href = "/home" | Yes | Standard redirect |
window.location.assign(url) | window.location.assign("/home") | Yes | Explicit navigation method |
window.location.replace(url) | window.location.replace("/home") | No | Login/logout/protected pages |
jQuery vs plain JavaScript
| Option | Purpose |
|---|
Cheat Sheet
// Standard redirect
window.location.href = "/home";
// Equivalent redirect method
window.location.assign("/home");
// Redirect without keeping current page in history
window.location.replace("/home");
// Delayed redirect
setTimeout(() => {
window.location.href = "/home";
}, 1000);
Quick rules
- Use
window.location.hreffor most redirects. - Use
window.location.replace()when the user should not return to the previous page with Back. - jQuery is not needed for redirecting.
- Use relative URLs like
/dashboardfor internal pages. - Use full URLs like
https://example.comfor external sites. - Be careful with redirect loops.
- Prefer server-side redirects when the page should always redirect.
Useful reminder
window.location represents the current browser URL.
Changing it tells the browser to navigate somewhere else.
FAQ
How do I redirect to another page in JavaScript?
Use:
window.location.href = "/new-page";
How do I redirect without allowing the user to go back?
Use:
window.location.replace("/new-page");
Is there a jQuery function for redirects?
No. jQuery does not provide a special redirect method. Use window.location.
What is the difference between href and replace()?
href keeps the current page in browser history. replace() removes it from history.
Can I redirect after a delay?
Yes:
setTimeout(() => {
window.location.href = "/next";
}, 2000);
Mini Project
Description
Build a small login-check page that redirects users based on whether they are signed in. This demonstrates conditional redirects, button-triggered redirects, and the difference between normal navigation and history-replacing navigation.
Goal
Create a page that sends signed-in users to a dashboard and signed-out users to a login page.
Requirements
- Create a boolean variable representing whether the user is logged in.
- If the user is logged in, redirect to
/dashboard. - If the user is not logged in, redirect to
/login. - Use
replace()for the login redirect. - Add a button that redirects to a help page when clicked.
Keep learning
Related questions
Deep Cloning Objects in JavaScript: Methods, Trade-offs, and Best Practices
Learn how to deep clone objects in JavaScript, compare structuredClone, JSON methods, and recursive approaches with examples.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers with clear examples.
How JavaScript Closures Work: A Beginner-Friendly Guide
Learn how JavaScript closures work with simple explanations, examples, common mistakes, and practical use cases for real code.