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.
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.