This example demonstrates how to assign an "onhashchange" event to a body element.
Click the button to change the anchor part of the current URL to #part5.
<!DOCTYPE html> <html> <body onhashchange="myFunction()"> <button onclick="changePart()">Test</button> <p id="demo"></p> <p id="demo1"></p> <script> function changePart() {//from w w w . j av a 2 s. c o m 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>
Bubbles: | Yes |
---|---|
Cancelable: | No |
Event type: | HashChangeEvent |
Supported HTML tags: | <body> |