javascript - jquery redirect with post - How do I redirect to another webpage?

jquery redirect to path / javascript / jquery / redirect

How can I redirect the user from one page to another using jQuery or pure JavaScript?

Peter Mortensen



Answer #1

JavaScript provides you many methods to retrieve and change the current URL which is displayed in browser's address bar. All these methods uses the Location object, which is a property of the Window object. You can create a new Location object that has the current URL as follows..

var currentLocation = window.location;

Basic Structure of a URL

<protocol>//<hostname>:<port>/<pathname><search><hash>

You can use the href property of the Location object.

window.location.href = "http://www.stackoverflow.com";

You can use assign() and replace methods also to redirect to other pages like these

location.assign("http://www.stackoverflow.com");

location.replace("http://www.stackoverflow.com");

You can change the location object href property using jQuery also like this

$(location).attr('href',url);