앵커 부분이 변경되면 onhashchange 이벤트가 발생합니다. 다음 코드를 실행하여 JavaScript에서 onhashchange 이벤트를 구현하는 방법을 배울 수 있습니다.
예시
<!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>