Javascript DOM HTML Anchor hash Property get

Introduction

The hash property sets or gets the hash of the href attribute value.

It is the part after the hash sign #.

Its value do not include the hash sign (#).

Return the anchor part of a link:

var x = document.getElementById("myAnchor").hash;

Click the button to display the anchor part of the link.

View in separate window

<!DOCTYPE html>
<html>
<body>
<p><a id="myAnchor" href="http://www.java2s.com/index.html#part2">Example link</a></p>
<p id="demo"></p>
<button onclick="myFunction()">Test</button>
<script>
function myFunction() {//from w w w  .  j  a va2s. c  o  m
  var x = document.getElementById("myAnchor").hash;
  document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>



PreviousNext

Related