Javascript location href property of DOM window object allows you to get or set the current URL. You can access the href property through location.href or window.location.href. Both methods will return the same result. If you are trying to assign the value retrieved from the Javascript location href to a variable or alert message box then it will return the current URL of the browser. And if you are trying to assign the value to the Javascript location href property then it will redirect the current URL to the specified location URL.
window.location.href = "URL";
or use directly
location.href = "URL";
Example 1: Get the current URL
<script language="javascript" type="text/javascript">
document.write(window.location.href);
</script>
OR
document.write(location.href);
Example 2: Set the new Location Href URL
You can also create the Javascript function to set the location href property by using onclick event of input type field or any other event that is supported by all the commonly used web browsers.
Be the first to rate this post
Tags: javascript, dom, javascript window object, 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
8/23/2008 8:47:10 PM