Javascript Reference - Location hash Property








The hash property sets or gets the anchor part of a URL, including the hash sign (#).

Browser Support

hash Yes Yes Yes Yes Yes

Syntax

Return the hash property.

var v = location.hash

Set the hash property.

location.hash=anchorname

Parameter Values

Value Type Description
anchorname String Set the anchor part of a URL




Return Value

A String type value representing the anchor part of the URL, including the hash sign (#).

Example

The following code shows how to Set the anchor part.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!-- w ww.java 2 s  .c o  m-->
<script>
function myFunction() {
    location.hash = "part";
    var x = location.hash;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: