In this article i am going to explain how to redirect a page using javaScript or JQuery.
It is better to use JavaScript to redirect a page than using JQuery as JavaScript is simple compared to JQuery.
Example:
You can use any of the following code to redirect a page using JavaScript
Both lines mentioned above redirects the page to location given in parenthesis. Only difference is window.locaiton.replace does not put the new page in session history. That means once you redirected to new page then you will not be able to go back to old page using browser's back button.
Where as if you use window.location.href new page will be stored in the session so that you can be able to go back to old page using browser's back button.
It is better to use JavaScript to redirect a page than using JQuery as JavaScript is simple compared to JQuery.
Example:
You can use any of the following code to redirect a page using JavaScript
// similar behavior as an HTTP redirect
window.location.replace("http://yourdomain.com");
// similar behavior as clicking on a link
window.location.href = "http://yourdomain.com";
Both lines mentioned above redirects the page to location given in parenthesis. Only difference is window.locaiton.replace does not put the new page in session history. That means once you redirected to new page then you will not be able to go back to old page using browser's back button.
Where as if you use window.location.href new page will be stored in the session so that you can be able to go back to old page using browser's back button.
I have added an example in JSFiddle. You can check in this link Redirect page using JavaScript
Redirect page using JQuery
Use the following code to redirect a page using JQuery
$(location).attr('href', 'http://yourDomain.com')
In this way you can redirect a page using JavaScript or JQuery.
For more posts on JavaScript visit JavaScript
For more posts on JQuery visit JQuery
No comments:
Post a Comment