onhashchange = myFunction(); - Javascript DOM Event

Javascript examples for DOM Event:Element Event Attribute

Description

The onhashchange event occurs when the URL anchor is about to change.

Summary

Bubbles Yes
Cancelable No
Supported HTML tags: <body>

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="changePart()">Try it</button>

<p id="demo"></p>

<script>
function changePart() {/*from  ww w .  j  a  va2 s  . c o m*/
    location.hash = "newAnchor";
    var x = location.hash;
    document.getElementById("demo").innerHTML = "The anchor part is now: " + x;
}

document.getElementsByTagName("BODY")[0].onhashchange = function() {myFunction()};

function myFunction() {
    console.log("The anchor part has changed!");
}
</script>

</body>
</html>

Related Tutorials