เมื่อส่วนสมอมีการเปลี่ยนแปลง จะเกิดเหตุการณ์ onhashchange คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อเรียนรู้วิธีใช้เหตุการณ์ onhashchange ใน JavaScript
ตัวอย่าง
<!DOCTYPE html>
<html>
<body onhashchange="newFunc">
<button onclick="functionChange()">Click to change anchor</button>
<script>
function functionChange() {
location.hash = "about";
var hash = location.hash;
document.write("The anchor part is now: " + hash);
}
// If the achor part is changed
function newFunc() {
alert("Anchor part changed!");
}
</script>
</body>
</html>