<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button onclick="clickHash()">hash</button>
<button onclick="clickCancel()">cancel</button>
<div style="height: 2000px; width: 1000px; background: red;"></div>
<button onclick="clickCancel()">cancel</button>
<script>
clickHash = function(){
location.hash = "#test";
window.scrollTo(0, 2000);
}
clickCancel = function(){
location.hash = "";
}
handler = function() {
console.log("hashchange fired");
}
window.onhashchange = handler;
</script>
</body>
</html>
What I expect by the code above is that when I click the hash button, change the hash and scroll the page to the bottom, and when I click the cancel button, remove the hash. However, when the hash is removed, the page will jump to the top. I guess it might be because the page get reload or refresh when the hash changed.
Does anyone know how to make the page not reload after changing the hash?
Here is the example.