tabs

Tuesday, November 12, 2013

How to redirect user to a new page using jquery

This is one of very frequent functions we see in every web application. Here in this post we will see how to redirect the user from the current location to another webpage using simple jQuery. In order to redirect user to another webpage we use the ‘location’ object available in JavaScript.
Below is the method in jQuery on how to redirect user to another webpage.

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


So whenever you like to redirect user to another webpage just use the location object as shown in the above code.
If you like to redirect user to a new webpage using JavaScript, follow the below lines of code.

window.location.replace("http://jquerybyexample.blogspot.com");
or
window.location.href = "http://jquerybyexample.blogspot.com";

Both the above methods redirect the user to a new webpage in the same way, but the only difference is when you use replace() the user gets redirected but the page will not be added in the browser history. So you cannot access the back button.
So it is recommended to use the second method to redirect user to a new webpage as it maintains the browser history too.
Hope you like this post and feel free to suggest and post your valuable comments.

No comments:

Post a Comment