views:

29

answers:

2

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?

+3  A: 

You can detect this event by using the in operator:

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

See also:

CMS
A: 

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.

mway