js part:
<script>
var lasturl=window.location.href;
function isHashChanged(){ var cururl=window.location.href; if(lasturl ==cururl ){ return false; } return true; } function hashChangeFire(){ console.info ("Changed"); } //url change listener if( ('onhashchange' in window) && ((typeof document.documentMode==='undefined') || document.documentMode==8)) { // The browser supports the onhashchange event window.onhashchange = hashChangeFire; // TODO, corresponding to the operation function performed by the new hash } else { // If it is not supported, use the timer detection method setInterval(function() {
// The function to detect whether the hash value or one of the segments has changed. The pointer retrieved through window.location.hash in the lower version of the iE browser is different from other browsers. Pay attention to
var ischanged = isHashChanged();
if(ischanged ) { hashChangeFire(); // TODO, corresponding to the operation function performed by the new hash } }, 150); } </script>
Test part:
I tested whether it worked through the anchor link
<a href="#aa">点我到a</a>
<a href="#bb">点我到b</a>
<div id="aa">aa</div>
<div id="aa">bb</div>