Javascript: onHashchange test

I am trying to check if the browser supports onHashChangeor not hide any code from it, if not, as follows:

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

I also tried:

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

As described in Quirksmode , this should work, but if I do , for example, in , which warns me, but Safari does not support : S alerttrue stateSafarionHashChange

What is the problem? If I do not return, how can I check this?

+5
source share
4 answers

This event can be detected using the operator in:

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

See also:

+22

, , (, "onhashchange" ).

@xkit , , IE7 onhashchange, true , if ( "onhashchange" ) {/code/} IE7 IE8.

@xkit, (, var isSet = true;) onhashchange. window.location.hash JavaScript , .

+6

, Safari, , onhashchange Quirksmode. ; , , , .

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

+1
source

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

+1
source

All Articles