How to Redirect to Another Web Page Using jQuery

This quick post shows how to redirect to another web page using jQuery.
Let's first see how to use vanilla javascript to redirect to another web page.
There are a couple of ways to redirect to another webpage with JavaScript. The most popular ones are location.href and location.replace:
Example:
// Simulate a mouse click:
window.location.href = "https://www.javaguides.net/";

// Simulate an HTTP redirect:
window.location.replace("https://www.javaguides.net/");

How to Redirect to Another Web Page Using jQuery

Refer below the single line of code to redirect from one page another page using JQuery:
$(location).attr('href','https://www.javaguides.net');
Here is a complete web page which redirects to another URL using jQuery:
<html>
   <head>
      <title>how to redirect to another web page using jQuery</title>
   </head>
   <body>
      <h2>how to redirect to another web page using jQuery</h2>

      <script
   src="https://code.jquery.com/jquery-3.4.1.min.js"
   integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
   crossorigin="anonymous"></script>
      <script>
         $(document).ready(function() {
            $(location).attr('href','https://www.javaguides.net');
         });
      </script>
   </body>
</html>
Open above web page in the browser will navigate to 'https://www.javaguides.net' web page using jQuery.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments