Javascript DOM onhashchange Event via HTML Tag onhashchange function

Introduction

In JavaScript:

object.onhashchange = function(){
       myScript};

This example uses the HTML DOM to assign an "onhashchange" event to a body element.

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

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="changePart()">Test</button>

<p id="demo"></p>
<p id="demo1"></p>
<script>
// Using the location.hash property to change the anchor part
function changePart() {/*from   w  w  w  . java2 s.c  o  m*/
  location.hash = "part5";
  var x = location.hash;
  document.getElementById("demo").innerHTML = "The anchor part is now: " + x;
}
document.getElementsByTagName("BODY")[0].onhashchange = function() {myFunction()};

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

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



PreviousNext

Related