JavaScript & jQuery Redirect Guide & Code Examples

A practical guide to implementing client-side page redirects with JavaScript and jQuery, including history state management and delayed transitions.

Client-Side Redirection with JavaScript

While server-side redirects (HTTP 301/302) are best for SEO, client-side JavaScript redirects are useful for interactive single-page apps, dynamic authentication workflows, form submissions, and device-specific routing.

1. Simulate HTTP Redirect (Recommended: location.replace)

Using window.location.replace() replaces the current document in the browser's session history. This prevents an infinite redirect loop when the user clicks the browser's "Back" button:

// Replaces history entry (Best practice) window.location.replace("https://www.rapidtables.online/web-design.html");

2. Standard Navigation (location.href)

Using window.location.href simulates clicking a link and preserves the referrer history:

// Standard navigation window.location.href = "https://www.rapidtables.online/web-design.html";

3. Timed Delayed Redirect

Redirect after a 3-second countdown using setTimeout:

setTimeout(function() { window.location.replace("https://www.rapidtables.online/web-design.html"); }, 3000); // 3000 ms = 3 seconds

4. jQuery Event-Driven Redirect

Trigger redirection after a button click or form submit event:

$("#myButton").click(function() { window.location.href = "https://www.rapidtables.online/web-design.html"; });