How to Redirect a Webpage using jQuery

Updated

Redirecting a user from one page to another or even from your website to a completely different one can be achieved in many ways. In this article, we will cover how to accomplish this task using the jQuery library but also how to do this with pure javascript.

How to Redirect a Webpage using jQuery

Redirecting a user to a different page using jQuery is a very simple one-liner. Remember, this solution requires the use of the jQuery library so you need to make sure it is available before running the code. The second method listed here using pure JavaScript is preferred for this reason.

jQuery Solution

$(location).attr('href','http://google.com');

Now let’s take a look at the slightly different but more preferred vanilla JavaScript version.

How to Redirect a Webpage using Pure Javascript

The vanilla JavaScript version of this code is also a simple one-liner.

window.location.href = "http://google.com";

That’s all there is to it. Now you know how to redirect a webpage using jQuery. However, unless you are determined to use jQuery go with the second pure JavaScript method.