Open In App
Related Articles

HTML onhashchange Event Attribute

Improve Article
Save Article
Like 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 a single value script and it runs when onhashchange event attribute is triggered. This attribute is associated with <body> tag only.



<!DOCTYPE html>
    <title>onhashchange event attribute</title>
        body {
            text-align: center;
        h1 {
            color: green;
    <h2>onhashchange event attribute</h2>
    <button onclick="changePart()">
        Try it
    <div id="gfg"></div>
        function changePart() {
            location.hash = "2";
            let 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
  • Firefox 3.6
  • Safari 5.0
  • Opera 10.6

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 04 Aug, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials