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.

Syntax for Javascript Window Location Href 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

<script language="javascript" type="text/javascript">
    document.write(location.href);
</script>

Example 2: Set the new Location Href URL

<script language="javascript" type="text/javascript">
    window.location.href = "http://www.ezineasp.net";
</script>

OR

<script language="javascript" type="text/javascript">
    window.location.href = "http://www.ezineasp.net";
</script>

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.

Continue to next tutorial: Javascript Location Hash with Example to learn how to bookmark hash name 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.