HTML | onhashchange Event Attribute
This attribute works when there has been changes to the anchor part. The anchor part starts with ‘#’ symbol of the current URL.
Syntax:
<element onhashchange = "script">
Attribute Value: This attribute contains single value script and it runs when onhashchange event attribute triggered. This attribute is associated with <body> tag only.
Example:
<!DOCTYPE html> <html> <head> <title>onhashchange event attribute</title> <style> body { text-align:center; } h1 { color:green; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h2>onhashchange event attribute</h2> <button onclick="changePart()">Try it</button> <div id="gfg"></div> <script> function changePart() { location.hash = "2"; var geeks = "Anchor part: " + location.hash; document.getElementById("gfg").innerHTML = geeks; } function myFunction() { alert("The anchor part has changed!"); } </script> </html> |
Output:

Supported Browsers: The browser supported by onhashchange event attribute are listed below:
- Chrome 5.0
- Internet Explorer 8.0
- Firefox 3.6
- Safari 5.0
- Opera 10.6
Recommended Posts:
- HTML | onkeydown Event Attribute
- HTML | onresize Event Attribute
- HTML | ondrop Event Attribute
- HTML | onkeyup Event Attribute
- HTML | ontoggle Event Attribute
- HTML | onmouseover Event Attribute
- HTML | ondragstart Event Attribute
- HTML | ononline Event Attribute
- HTML | onsubmit Event Attribute
- HTML | onclick Event Attribute
- HTML | onmouseout Event Attribute
- HTML | ondragenter Event Attribute
- HTML | oninvalid Event Attribute
- HTML | onunload Event Attribute
- HTML | ondrag Event Attribute
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh



