On - [removed].hash - Change?

后端 未结 13 1348
谎友^
谎友^ 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 14:10

    Note that in case of Internet Explorer 7 and Internet Explorer 9 the if statment will give true (for "onhashchange" in windows), but the window.onhashchange will never fire, so it's better to store hash and check it after every 100 millisecond whether it's changed or not for all versions of Internet Explorer.

        if (("onhashchange" in window) && !($.browser.msie)) {
             window.onhashchange = function () {
                  alert(window.location.hash);
             }
             // Or $(window).bind( 'hashchange',function(e) {
             //       alert(window.location.hash);
             //   });
        }
        else {
            var prevHash = window.location.hash;
            window.setInterval(function () {
               if (window.location.hash != prevHash) {
                  prevHash = window.location.hash;
                  alert(window.location.hash);
               }
            }, 100);
        }
    

    EDIT - Since jQuery 1.9, $.browser.msie is not supported. Source: http://api.jquery.com/jquery.browser/

    0 讨论(0)
提交回复
热议问题