Javascript location replace function of DOM window object provides the functionality to replace the current URL with new location URL and also allows you to redirect the current URL. You can call the Javascript location replace function using any event of form input fields such as input type button onclick event to replace the current URL with new URL. You can also code the location replace function in the Javascript block of the HTML <head> head section to call the location replace function on body load automatically to redirect the current URL to new URL. Any of the location.replace or window.location.replace can be used in the Javascript code.
location.replace("URL");
or
window.location.replace("URL");
Javascript location replace function accepts 1 parameter of string value type as new location URL path.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title>Javascript window location replace</title>
<script type="text/javascript" language="javascript">
{
}
</script>
</head>
<input type="button" value="Location Replace URL" onclick="redirect();" />
</html>
Above example code for Javascript window location replace function will redirect to new URL onclick event of input type button of HTML document.
Be the first to rate this post
Tags: javascript, dom, javascript window object, javascript location replace, javascript location href, javascript window location object, javascript location url, javascript location hash, javascript location host, javascript location hostname, javascript location port, javascript location protocol, javascript location search, javascript location pathname, javascript location reload, javascript location replace url