How to Redirect user from one page to another using JQuery?

Is it possible to redirect my website visitor from one page to another using JQuery?

and how can i do that?



I don’t think you will have to use any JQuery at all.

there are two methods to redirect from a page to another using javascript alone:

Either using document.location.href:

document.location.href = "Your new page url";
  • This will cause similar behavior to Clicking on a link on your website. Just keep in mind that using document.location.href will cause for that HTTP request to be listed in the session history for your browser as an explicit request. meaning that it will be visible when user clicks (back and forward) buttons and that might cause some unwanted confusion for the visitor.

Or using document.location.replace():

document.location.replace("Your new page url");
  • This might be the best solution for you, as it does an implicit redirect and doesn’t list the HTTP request in the session history of your browser.

In your case i think you should go with document.location.replace() as that will make an internal redirect without confusing the visitors of your website.


If JQuery is required, you can do it this way:

$(location).attr('href', '')

But it’s highly recommended that you use the Javascript pure solution suggested by @isoftech .