Monday, March 19, 2012

How to redirect user to another web page using jQuery

Simple jQuery code to redirect user to another web page. To redirect, need to use location object available in JavaScript.

Earlier I had posted about Redirect to another page after X seconds wait, Different ways to refresh or reload page using jQuery, Get URL Parameters using jQuery and How to force browser to reload the images. And in this post, find jQuery way to redirect user to another web page.

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


If you are using JavaScript then you can use any of the two methods mentioned in below code.
//Method 1
window.location.replace("http://jquerybyexample.blogspot.com");
//Method 2
window.location.href = "http://jquerybyexample.blogspot.com";
//
But you should not use location.replace() to redirect as it does not add the page in the browser's history. So the back button will not work.

Feel free to contact me for any help related to jQuery, I will gladly help you.