In JavaScript, using the addEventListener()
method:
object.addEventListener("hashchange",
myScript);
This example uses the addEventListener()
method to attach a "hashchange" event to the window object.
Click the button to change the anchor part of the current URL to #part5.
<!DOCTYPE html> <html> <body> <button onclick="changePart()">Test</button> <p id="demo"></p> <p id="demo1"></p> <script> function changePart() {//from ww w . ja v a2s .c o m location.hash = "part5"; var x = location.hash; document.getElementById("demo").innerHTML = "The anchor part is now: " + x; } window.addEventListener("hashchange", 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> |