HTML onhashchange 이벤트 속성은 HTML 문서에서 URL의 앵커 부분에 변경이 있을 때 트리거됩니다.
구문
다음은 구문입니다 -
<tagname onhashchange=”script”></tagname>
HTML onhashchange 이벤트 속성의 예를 살펴보겠습니다-
예시
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
height: 100vh;
background-color: #FBAB7E;
background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);
text-align: center;
}
.btn {
background: #db133a;
border: none;
color: #fff;
height: 2rem;
padding: 8px 10px;
}
</style>
</head>
<body onhashchange="hashChange()">
<h1>HTML onhashchange Event Attribute Demo</h1>
<button class="btn" onclick="change()">Set Hash</button>
<p class="msg"></p>
<script>
function change() {
location.hash = "newHashPart";
document.querySelector('.msg').innerHTML = 'The hash part of the URL is: ' + location.hash;
}
function hashChange() {
document.body.style.background = 'lightblue';
}
</script>
</body>
</html> 출력

"해시 설정을 클릭합니다. ” 버튼을 눌러 새 해시를 현재 URL로 설정하고 onhashchange 이벤트 속성이 작동하는 방식을 관찰합니다.
