2010-06-22 109 views
75

我使用:jQuery的 - hashchange事件

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

绑定到哈希改变事件的功能。这似乎适用于IE8,Firefox和Chrome,但不适用于Safari,我假设不在早期版本的IE中。对于这些浏览器,我想禁用使用哈希和hashchange事件的JavaScript代码。

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

+3

[jQuery hashchange事件](http://benalman.com/projects/jquery-hashchange-plugin/) - 即使在IE8中,jQuery插件也是完美的。 +它很容易使用:) – enloz 2011-09-16 03:38:59

回答

17

有一个hashchange插件,它封装了功能性和跨浏览器发出available here

+0

只适用于 2016-04-14 10:09:55

2

请注意,在IE 7和IE 9的情况下,如果statment会为(窗口中的“onhashchange”)赋予true,但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); 
    } 
+2

浏览器处理这不是太多吗?每100毫秒轮询一次哈希变化? – adardesign 2011-07-06 17:53:26

+5

你的示例代码使我的IE8警报,直到我打开任务管理器并杀死进程:) – enloz 2011-09-16 03:29:45

+0

这是因为有一个错字,而不是“storedHash”使用“prevHash”,它会工作。他基本上使用了一个不同的变量名称。 – Nick 2013-09-26 05:57:21

17

一种不同的方法,您的问题...

有到hashchange事件绑定到方法3种方式:

<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的所有工作

7

尝试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; 
3

我只是遇到了同样的问题(在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> 
+0

你可以使用'$('a [href^=“#”]')来获得以散列开头的'href'的链接,避免需要类添加 – tobymackenzie 2016-09-21 04:27:11

0

这里是@ 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); 
} 
2

怎么样用不同的方式代替哈希事件并且像popstate一样听popstate。

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

这种方法在我到目前为止尝试过的大多数浏览器中工作正常。

+1

Popstate甚至比hashchange更新。例如,它在IE <10中不被支持。 – 2015-12-30 18:34:53

4

如果有人需要它,2017年更新的答案是onhashchange在所有主流浏览器中都得到很好的支持。详情请参阅caniuse。要与jQuery使用它无需插件:

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

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

+0

这个效果很好,使用'window.location.hash'来访问当前散列。 – 2018-03-08 10:16:56