Example:
HTML
<!DOCTYPE html>
<html>
<body onhashchange="myFunction()">
<p>Click the button to change the anchor part of the current URL to #part5</p>
<button onclick="changePart()">Try it</button>
<p id="demo"></p>
<script>
// Using the location.hash property to change the anchor part
function changePart() { location.hash = "part5"; let x = "The anchor part is now: " + location.hash; document.getElementById("demo").innerHTML = x;
}
// Alert some text if there has been changes to the anchor part
function myFunction() { alert("The anchor part has changed!");
}
</script>
</body>
</html>
How to add a JavaScript when the anchor part has been changed | HTML Window Event Attributes |
HTML onafterprint Event Attribute | HTML Window Event Attributes |
HTML onbeforeprint Event Attribute | HTML Window Event Attributes |
HTML onbeforeunload Event Attribute | HTML Window Event Attributes |
HTML onerror Event Attribute | HTML Window Event Attributes |
HTML onhashchange Event Attribute | HTML Window Event Attributes |
HTML onload Event Attribute | HTML Window Event Attributes |
HTML onoffline Event Attribute | HTML Window Event Attributes |
HTML ononline Event Attribute | HTML Window Event Attributes |
HTML onpageshow Event Attribute | HTML Window Event Attributes |
HTML onresize Event Attribute | HTML Window Event Attributes |
HTML onunload Event Attribute | HTML Window Event Attributes |
Type
: |
html |
Category
: |
Web Tutorial |
Sub Category
: |
HTML Window Event Attributes |
Uploaded by
: |
Admin |