With the jQuery API, you can also redirect web pages to another URL in one line of code. While this is not an right way to use jQuery, it makes sense to take advantage of every feature we can use. because we all use jQuery in our web projects to meet different needs. You can use jQuery's attr() function to redirect the web page as follows:

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>

LEAVE A REPLY

Please enter your comment!
Please enter your name here

eleven + 14 =