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.

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

 <html>
<head>
<title>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 = "https://web-help-technology.blogspot.com/"; 
$(location).attr('href',url);
 }); 
 </script> 
</body> 
</html>

No comments:

Post a Comment

Python in AI: Getting Started with TensorFlow & PyTorch

  Artificial Intelligence (AI) is no longer just a buzzword. It powers recommendation systems, chatbots, self-driving cars, and more. At the...