Javascript:onHashchange 测试

发布于 2024-09-29 15:55:35 字数 607 浏览 5 评论 0原文

我正在尝试检查浏览器是否支持 onHashChange 或如果不支持则不隐藏某些代码,以这种方式:

if(window.onhashchange){
    ...code...
} else {
   ...other code...
}

我也尝试过此操作:

if(typeof window.onhashchange === "function"){
    alert("Supports");  
} else {
    alert("Doesn't Supports");  
}

Quirksmode 这应该可以工作,但是如果我在 alert 中执行例如 < Safari 中的 code>true state 会提醒我,但 Safari 不支持 onHashChange :S

它有什么问题?如果我的方法不对,我该如何检查?

I'm trying to check if the browser supports onHashChange or not to hide some code from it if not, in this way:

if(window.onhashchange){
    ...code...
} else {
   ...other code...
}

I tried this too:

if(typeof window.onhashchange === "function"){
    alert("Supports");  
} else {
    alert("Doesn't Supports");  
}

As described on Quirksmode this should work but if I do an alert for example in true state in Safari than alerts me but Safari is not supporting onHashChange :S

What's the problem with it? If I'm not on the right way how should I check it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

臻嫒无言 2024-10-06 15:55:35

您可以使用 in 运算符检测此事件:

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

另请参阅:

You can detect this event by using the in operator:

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

See also:

难以启齿的温柔 2024-10-06 15:55:35

请注意,最好使用特征检测而不是存在推断(例如窗口中的“onhashchange”)。

@xkit 向我解释了一个很好的功能测试,可以解决这样一个事实:尽管 IE7 不支持 onhashchange,但对于存在推断,例如 if("onhashchange" in window){/code ,它仍然会返回 true /} 在 IE8 中使用 IE7 标准文档模式时。

@xkit 建议在 onhashchange 事件的处理函数中设置一个标志(例如 var isSet = true;)。然后使用 JavaScript 更改 window.location.hash 并查看是否设置了该标志。

Be warned that you're better off using feature detection rather than existence inference (such as "onhashchange" in window).

@xkit explained to me a good feature test to work around the fact that although IE7 doesn't support onhashchange it would still return true for existence inference such as if("onhashchange" in window){/code/} when using IE7 Standard Document Mode in IE8.

What @xkit suggested was setting a flag (such as var isSet = true;) within a handler function for the onhashchange event. Then changing window.location.hash using JavaScript and see if the flag was set.

遇到 2024-10-06 15:55:35

自 Quirksmode 文章撰写以来,您所使用的 Safari 版本可能已添加了对 onhashchange 事件的支持。测试应该仍然有效;在您知道不支持该事件的其他浏览器中尝试一下。

编辑:另外,您应该使用@CMS描述的方法,因为默认情况下该事件不包含函数;因此这两个测试都会失败。

It's likely that the version of Safari that you're using has added support for the onhashchange event since the time that that Quirksmode article was written. Tests should still be valid; try it in other browsers you know not to support the event.

Edit: also, you should use the method described by @CMS instead, as the event will not contain a function by default; thus both of those tests will fail.

叹梦 2024-10-06 15:55:35

if (window.onhashchange !== undefined)alert('支持 onhashchange');

if (window.onhashchange !== undefined) alert('Supports onhashchange');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文