revision:
The attribute fires when there has been changes to the anchor part (begins with a '#' symbol) of the current URL.
The "onhashchange attribute" is part of the event attributes and can be used on the following element:<body>.
<body onhashchange="script"></body >
value: this attribute contains single value script and it runs when onhashchange attribute is triggered. This attribute is associated with <body> tag only
example
Click the button to change the anchor part of the current URL to #part5
codes:
<body onhashchange="firstChange()"> <p class="spec">Click the button to change the anchor part of the current URL to #part5</p> <button style="margin-left:3vw;" onclick="changePart()">try it</button> <p class="spec" id="demo"></p> </body> <script> // Using the location.hash property to change the anchor part function changePart() { location.hash = "part5"; var x = "The anchor part is now: " + location.hash; document.getElementById("demo").innerHTML = x; } // Alert some text if there has been changes to the anchor part function firstChange() { alert("The anchor part has changed!"); } </script>
example
codes:
<h3>onhashchange event attribute</h3> <button id="two" style="margin-left:3vw;" onclick="changeHash()">click me</button><br><br> <div style="margin-left:3vw;" id="mode"></div> <script> function changeHash() { location.hash = "2"; var y = "anchor part: " + location.hash; document.getElementById("mode").innerHTML = y; } </script>