5 ways to redirect a web page using JavaScript and jQuery

When an HTTP request for one page automatically goes to another page then it is called a redirection. The redirection of a web page is used for different reasons e.g. redirecting to a different domain e.g. when you move your website from one URL to another URL or redirecting to a more recent version of a page you are looking at it. There are many ways to redirect a web page and different web technologies provide different ways e.g. Java provides sendRedirect() and forward() method for redirection. The redirection can also be divided into two categories, client side and server side. In client side, redirection client is responsible for routing request to another URL but in server side redirection, its server's job to redirect to a new page.

The jQuery and JavaScript are mostly used for client side redirection and there are a number of ways to redirect a web page using JavaScript In this tutorial, we'll examine some of the most popular and common ways to redirect a web page using JavaScript and jQuery.



How to redirect a web page using JavaScript

Before looking at the jQuery code, let's first learn how to redirect a web page by using plain JavaScript. It provides several simple ways to achieve this task without loading an external JavaScript library like jQuery.


Redirection using window.location.href of Javascript

The most common way to redirect an URL using JavaScript is just setting the href attribute of the window.location object. This simulates normal navigation to a new page. I don't think any task can be simpler than this, just assign a new URL. For example, if you are redirecting URL A to URL B then just put the URL B here as shown in the following example:


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


Tip: You should remember that window.location.href loads page from browser's cache and does not always send the request to the server. So, if you have an old version of the page available in the cache then it will redirect to there instead of loading a fresh page from the server.


Redirection using replace() function

Another way to redirect to a web page in jQuery is by using the replace() function of the window.location object. This method removes current URL from the history and replaces it with a new URL. Here is an example of how to use this function for redirection:

window.location.replace("url"); 


Tip: Use the replace() method if you want to want to redirect to a new page and don't allow the user to navigate to the original page using the back button.



Redirection using window assign() function

You can also use the window.location.assign() function to redirect a web page using JavaScript. This function adds new URL to the history stack and redirected to the new URL, which means, unlike replace() function you can use the back button to go back to the original document. Here is how you can use this method to redirect to a new url

window.location.assign("url"); 

Tip: Use the assign() method for redirection if you want to allow the user to use the back button to go back to the original document.


Redirection using navigate function

This function is same as window.location="url" and it also loads the page from the cache instead of directly requesting it from the server. Here is an example to use the navigate() function for redirection to a new page

window.navigate("page.html");

This code is similar to the first method and the page will be loaded from the cache if available instead of sending a new request to the server.



How to redirect web page using jQuery

The jQuery API also allows you to redirect a web page to another URL in just one line of code. Though this is not the ideal way of using jQuery and but since we all use jQuery for different needs in a web project, it makes sense to leverage it for whatever feature we can. You can use the attr() function of jQuery to redirect a web page as shown below:

var url = "http://google.com";
$(location).attr('href',url);

There is a slight difference in using attr() vs window.location.href of JavaScript. The $(location).attr('href', url) will request a fresh page from server, while window.location.href loads pages from cache.

Here is a complete web page which redirects to another URL using jQuery:

<html>
   <head>
      <title>How to redirect a webpage using jQuery</title>
   </head>
   <body>
      <h2>Redirecting to another URL ..</h2>

      <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
      <script>
         $(document).ready(function() {
         
          var url = "http://javarevisited.blogspot.com";
          $(location).attr('href',url);
         
         });
      </script>
   </body>
</html>

and here is the output of this web page when you run it on a web browser e.g. internet explorer, chrome or edge:

10 ways to redirect a web page using JavaScript and jQuery


You can see that page is redirecting, once the redirect will be complete, the URL and page title will also be replaced by the target URL.  See Head First jQuery to learn more about jQuery and its powerful API.



How to redirect to the previous page

Another case of redirection is going to the previous page, here are some of the common way to go to the previous page using JavaScript's window object:

window.history.back(); // Simulates a back button click
window.history.go(-1); // Simulates a back button click
window.history.back(-1); // Simulates a back button click

All three methods simulate a back button click programmatically. You can use any of the methods you want to use.


That's all about how to redirect a web request to another web page using JavaScript and jQuery. As you have seen there are multiple ways to redirect but there is subtle differences between them e.g. $(location).attr('href', url) call of jQuery will open a fresh page from the server, but the Javascript window.location.href loads pages from cache. Similarly, the window.location.assign(url) adds new URL to the document history and redirected to the new URL but window.location.replace("URL") removes current URL from history and replaces it with a new URL.


Further Reading


Other jQuery tutorials you may like to explore
  • 5 Books to learn jQuery for Web developers (books)
  • How to get current URL, Parameters, and Hash using jQuery? (solution)
  • How to modify more than one element in one line? (example)
  • jQuery Hello World Example (program)
  • Free jQuery eBooks and PDFs (download)
  • How to use more than one jQuery UI DatePicker in JSP page? (example)
  • How to create tab-based UI using jQuery? (example)
  • Top 20 jQuery Interview Questions for Java Web Developers (list)
  • How to check/uncheck checkboxes in jQuery (solution)
  • How to download jQuery dynamically in a web page? (solution)

Thanks for reading this article. If you like this article then please share with your friends and colleagues. If you have any question or feedback then please drop a comment.

No comments:

Post a Comment