繁体   English   中英

jQuery - hashchange事件

[英]jQuery - hashchange event

我在用:

$(window).bind( 'hashchange', function(e) { });

将函数绑定到散列更改事件。 这似乎适用于IE8,Firefox和Chrome,但不适用于Safari,我认为不是早期版本的IE。 对于这些浏览器,我想禁用使用hash和hashchange事件的JavaScript代码。

有没有一种方法可以检测浏览器是否支持hashchange事件? 也许与jQuery.support ...

您可以通过以下方式检测浏览器是否支持该事件:

if ("onhashchange" in window) {
  //...
}

也可以看看:

如果有人需要,2017年的更新答案是,所有主流浏览器都支持onhashchange 有关详细信息,请参阅caniuse 要与jQuery一起使用,不需要插件:

$( window ).on( 'hashchange', function( e ) {
    console.log( 'hash changed' );
} );

偶尔我会遇到仍然使用hashbang URL的遗留系统,这很有帮助。 如果您正在构建新的东西并使用哈希链接,我强烈建议您考虑使用HTML5 pushState API。

有一个hashchange插件,它包含了这里提供的功能和跨浏览器问题。

解决问题的另一种方法......

有三种方法可以将hashchange事件绑定到方法:

<script>
    window.onhashchange = doThisWhenTheHashChanges;
</script>

要么

<script>
    window.addEventListener("hashchange", doThisWhenTheHashChanges, false);
</script>

要么

<body onhashchange="doThisWhenTheHashChanges();">

这些都适用于Win 7上的IE 9,FF 5,Safari 5和Chrome 12。

试试Mozilla官方网站: https//developer.mozilla.org/en/DOM/window.onhashchange

引用如下:

if ("onhashchange" in window) {
    alert("The browser supports the hashchange event!");
}

function locationHashChanged() {
    if (location.hash === "#somecoolfeature") {
        somecoolfeature();
    }
}

window.onhashchange = locationHashChanged;

我刚遇到同样的问题(在IE7中缺少hashchange事件)。 适合我的目的的解决方法是绑定哈希更改链接的click事件。

<a class='hash-changer' href='#foo'>Foo</a>

<script type='text/javascript'>

if (("onhashchange" in window) && !($.browser.msie)) { 

    //modern browsers 
    $(window).bind('hashchange', function() {
        var hash = window.location.hash.replace(/^#/,'');
        //do whatever you need with the hash
    });

} else {

    //IE and browsers that don't support hashchange
    $('a.hash-changer').bind('click', function() {
        var hash = $(this).attr('href').replace(/^#/,'');
        //do whatever you need with the hash
    });

}

</script>

如何使用不同的方式而不是哈希事件,并听取popstate之类的。

window.addEventListener('popstate', function(event)
{
    if(window.location.hash) {
            var hash = window.location.hash;
            console.log(hash);
    }
});

这种方法在我迄今为止尝试的大多数浏览器中都能正常工作

请注意,在IE 7和IE 9的情况下,如果statment将给出true(Windows中的“onhashchange”),但window.onhashchange将永远不会触发,因此最好存储散列并在每100毫秒后检查它是否更改适用于所有版本的IE。

    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);
    }

这个小小的jQuery插件使用起来非常简单: https//github.com/finnlabs/jquery.observehashchange/

使用Modernizr检测功能。 通常,jQuery提供检测浏览器功能: http//api.jquery.com/jQuery.support/ 但是,hashchange不在列表中。

Modernizrwiki提供了一个库列表,用于向旧浏览器添加HTML5功能。 hashchange列表包含一个指向项目HTML5 History API的指针,它似乎提供了在旧浏览器中模拟行为时所需的功能。

这是@ johnny.rodgers的更新版本

希望帮助某人。

// ie9 ve ie7 return true but never fire, lets remove ie less then 10
if(("onhashchange" in window) && navigator.userAgent.toLowerCase().indexOf('msie') == -1){ // event supported?
    window.onhashchange = function(){
        var url = window.location.hash.substring(1);
        alert(url);
    }
}
else{ // event not supported:
    var storedhash = window.location.hash;
    window.setInterval(function(){
        if(window.location.hash != storedhash){
            storedhash = window.location.hash;
            alert(url);
        }
    }, 100);
}

我认为Chris Coyier有解决这个哈希问题的方法,看看他的截屏视频:

动态内容的最佳实践

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM