How to redirect to another webpage with JavaScript

JavaScript is very extensive. While working with client side, you may occasionally want to redirect client after Ajax request success, e.g., redirect to dashboard after login successful. If you want to redirect to another page you have two options.

location.href

If you want to simulate like someone clicking on a link, use location.href

// same as user clicking on a link
window.location.href = "https://hackthestuff.com";

location.replace

If you want to simulate an HTTP redirect, use location.replace

// same as an http redirect
window.location.replace("https://hackthestuff.com");

JQuery $(location)

You can also redirect using JQuery library.

$(location).attr('href', 'https://hackthestuff.com');

 

Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

Or

No Comment