Skip to content
Related Articles

Related Articles

HTML | onhashchange Event Attribute

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 16 Aug, 2022
View Discussion
Improve Article
Save Article

This attribute works when there has been changes to the anchor part. The anchor part starts with ‘#’ symbol of the current URL.
Supported Tags 

  • <body>


<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.


<!DOCTYPE html>
        <title>onhashchange event attribute</title>
            body {
            h1 {
        <h2>onhashchange event attribute</h2>
        <button onclick="changePart()">Try it</button>
        <div id="gfg"></div>
        function changePart() {
            location.hash = "2";
            var geeks = "Anchor part: " + location.hash;
            document.getElementById("gfg").innerHTML = geeks;
        function myFunction() {
            alert("The anchor part has changed!");


Supported Browsers: The browser supported by onhashchange event attribute are listed below: 

  • Chrome 8.0
  • Edge 12.0
  • Internet Explorer 8.0
  • Firefox 3.6
  • Safari 5.0
  • Opera 10.6


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!