Redirect to Another Page in jQuery and Javascript

Last Updated on April 26, 2024 by Roshan Parihar

To redirect to another page automatically in Javascript, use the window.location.replace() and pass the URL as an argument.

You can also redirect to another page in jQuery using $(window).attr("location","url") function and pass your url as an argument. Let’s find out with useful examples given below.

Redirect to Another Page Using window.location.replace() in Javascript (Behaviour as HTTP Redirect)

When you put the below code to your website page. It will automatically take you to another page whose url is mentioned in the window.location.replace() as argument. You just have to mention the function inside the setTimeout() function for some delay.

It behaves as HTTP Redirect and does not save any history to the browser. If you press the back button, you will not get redirection to the previous page.

Example 1

The above code is a javascript code for a redirect to another page. It takes a 10-second delay to take you to another page.

Bonus: download a Free jQuery cheat sheet that will show you 20+ most important examples to learn in jQuery.

Redirection Using window.location.href() in Javascript (Behaviour as Link)

The below example works as a link redirection. It gives you the same behavior as an anchor link work. Place any URL in the window.location.href=" part of the below code. The redirection will not trigger until you press the button.

Example 2

Output



Click the button given in the example above. It takes time delay of 3 seconds to take to the mentioned URL page.

jQuery Auto-redirection Using $(window).attr(“location”,”url”) (Behaviour as HTTP Redirect)

If you want to perform the auto-redirection to another page using jQuery, use the $(window).attr("location","url") to perform this task. Change the URL with the URL where you want to make redirection.

Example 3

The above example of jQuery takes 10 seconds to make redirection. You will automatically get redirected to the mentioned URL.

With $(window).attr(“href”,’url’) in jQuery (Behaviour as Link)

If you want to create a button with the behavior same as an anchor link, Use the $(window).attr("href",'url') to perform this task using jQuery. Change the ‘url’ where you url for redirection.

Example 4

Output



Click the above button to redirect to another page using jQuery. There is no time delay given in the above example. You will be redirected to another page immediately with button click.

DOWNLOAD Free jQuery CHEAT SHEET

You may also like to read.