On - [removed].hash - Change?

后端 未结 13 1385
谎友^
谎友^ 2020-11-21 13:38

I am using Ajax and hash for navigation.

Is there a way to check if the window.location.hash changed like this?

http://example.com/blah

13条回答
  •  梦谈多话
    2020-11-21 13:50

    var page_url = 'http://www.yoursite.com/'; // full path leading up to hash;
    var current_url_w_hash = page_url + window.location.hash; // now you might have something like: http://www.yoursite.com/#123
    
    function TrackHash() {
        if (document.location != page_url + current_url_w_hash) {
            window.location = document.location;
        }
        return false;
    }
    var RunTabs = setInterval(TrackHash, 200);
    

    That's it... now, anytime you hit your back or forward buttons, the page will reload as per the new hash value.

提交回复
热议问题