Javascript DOM onhashchange Event

Introduction

Execute a JavaScript when the anchor part has been changed:

Click the button to change the anchor part of the current URL to #part5

View in separate window

<!DOCTYPE html>
<html>
<body onhashchange="myFunction()">


<button onclick="changePart()">Test</button>

<p id="demo"></p>

<p id="demo1"></p>
<script>
function changePart() {/*from ww  w.  ja  v a 2s . c om*/
  location.hash = "part5";
  var x = location.hash;
  document.getElementById("demo").innerHTML = "The anchor part is now: " + x;
}

function myFunction() {
  document.getElementById("demo1").innerHTML = "The anchor part has changed!";
}
</script>

</body>
</html>

The onhashchange event occurs when changing the anchor part of the current URL.

Bubbles: Yes
Cancelable: No
Event type: HashChangeEvent
Supported HTML tags: <body>



PreviousNext

Related