Javascript Location Redirect Function
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.
JavaScript Window Location Object Examples:
You can see the live samples and examples of JavaScript Window Location Object from the following links:
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.
Output:
You can see the output of above discussed code from the following link:
Continue to next tutorial: Javascript Window Location Reload or Refresh Function to learn how refresh the current web page using JavaScript.
