Javascript Location redirect function can be generated by using any of the location href or window location replace function to redirect the old URL to new location URL. Javascript location href property can be used to set the new URL for href property that could redirect to new URL. Javascript location replace function also performs the similar task that accepts the string type value as new location URL to redirect the current URL to new URL. Just create a Javascript function with name redirect() and place one of these functions to generate the Javascript Redirect function.

Syntax for Creating Javascript Redirect Function

location.href = "new URL";

It will assign the new location URL to the current window URL.

location.replace("new URL");

This function also replaces the current URL with new location URL parameter passed to the location replace function.

Examples of Javascript Redirect Function 

function redirect() {
    location.replace("http://www.ezineasp.net");
}

OR

function redirect() {
    location.href = "http://www.ezineasp.net";
}

You can any of the above Javascript Redirect Function to redirect the old URL to new location URL.

Call the above redirect function onclick event of the button:

<input
    type="button"
    value="Redirect Location URL"
    onclick="redirect();" />

You can also redirect the current URL on page load without any click event and even without loading the document.

Continue to next tutorial: Javascript Window Location Reload or Refresh Function to learn how refresh the current web page using JavaScript.

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.