admin管理员组文章数量:1192145
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?
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?
Share Improve this question edited Oct 27, 2010 at 6:22 Adam Halasz asked Oct 27, 2010 at 6:05 Adam HalaszAdam Halasz 58.3k67 gold badges153 silver badges216 bronze badges4 Answers
Reset to default 21You can detect this event by using the in
operator:
if ("onhashchange" in window) {
//...
}
See also:
- onhashchange - MDC
- Detecting event support without browser sniffing
- Emulating
onhashchange
without setInterval
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.
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.
if (window.onhashchange !== undefined) alert('Supports onhashchange');
本文标签: cross browserJavascriptonHashchange TestStack Overflow
版权声明:本文标题:cross browser - Javascript : onHashchange Test - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738411198a2085332.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论