Skip to content
Related Articles

Related Articles

Improve Article

HTML | onhashchange Event Attribute

  • Difficulty Level : Easy
  • Last Updated : 30 Jul, 2021

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 5.0
  • Internet Explorer 8.0
  • Firefox 3.6
  • Safari 5.0
  • Opera 10.6


Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :