Return the anchor part of a URL.
var x = location.hash;
Click the button to set the anchor part of the current URL.
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {//from ww w .ja va 2 s . co m location.hash = "part5"; var x = "The anchor part is now: " + location.hash; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The hash property sets or gets the anchor part of a URL, including the hash sign (#).
When setting this property, do not include the hash sign (#).
Property Values
Value | Type | Description |
---|---|---|
anchorname | String | Specifies the anchor part of a URL |
The hash property returns a String representing the anchor part of the URL, including the hash sign (#).