URL Redirection

JavaScript URL Redirection

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.

Javascript URL redirection

In Javascript, you can use many methods to redirect a web page to another one. Almost all methods are related to window.location object, which is a property of the Window object. It can be used to get the current URL address (web address) and to redirect the browser to a new page.

window.location = "http://www.corelangs.com/css"

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.

Full source

<!DOCTYPE html> <html> <head> <script> function newLocation() { window.location="http://www.w3.org"; } </script> </head> <body> <input type="button" value="Go to new location" onclick="newLocation()"> </body> </html>

URL Redirection on page load

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.

<html> <head> <script type="text/javascript"> function loadNewDoc(){ window.location="http://www.w3.org"; } </script> </head> <body onLoad="setTimeout('loadNewDoc()', 5000)"> <h1>This page will be redirected to new location....</h1> </body> </html>

Other URL redirection methods

JavaScript provides you many methods to redirect a URL to another page. windows.location Object also have another three methods also. They are location.replace(), location.assign() and location.reload().


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

URL Redirection 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

In some case, if someone wants to redirect back to home page then he may use the following javascript code.

window.location = window.location.host

URL redirection and Search Engine Optimization

URL redirection and Search Engine Optimization

If you want to notify the search engines (SEO) about your URL forwarding, you should add the rel="canonical" meta tag to your website head part because search engines don't analyze javascript to check the redirection.

<link rel="canonical" href="http://corelangs.com/" />