URL redirection, also called URL forwarding, is a way to automatically redirect a web page to another web page. The redirected page is often on the same website, or it can be on a different web site or a web server.
window.location and window.location.href
In many places you have seen using window.location.href instead of window.location. Both usage are same in terms of behavior. window.location returns an object. If .href is not set, window.location defaults to change the parameter .href.
URL Redirection on page load
If you want to redirect your page to a new location while on page load, you can use the following code. The following program shows a message for 5 second and redirected to new location.
The other most frequently using method is the replace() method of window.location object, it will replaces the current document with a new one. In replace() method, you can pass new URL to replace() method and it will perform an HTTP redirect.
The location.assign() method loads a new document in the browser window.
When to use assign() and replace() method
The difference between assign() and replace() method is that the location.replace() method delete the current URL from document history, so it is unable to navigate back to the original document. You can't use the browsers "Back" button in this case. If you want to avoid this situation, you should use location.assign() method, because it is load a new Document in browser.
The location.reload() method reloads the current document in the browser window.
The window.navigate() method is similar to assigning a new value to the window.location.href property. Because it is only available in MS Internet Explorer, so you should avoid using this in cross-browser development.
Back to Home Page
URL redirection and Search Engine Optimization