4

I want to check if the browser supports hashchange event and, if not, fake it with setInterval.

I have tried

if ('onhashchange' in window) {
    window.onhashchange = hashChange;
} else {
    /* setInterval graceful degradation */
}

But the problem is that, in IE8 in IE7 Compat mode, 'onhashchange' in window returns true because window.onhashchange is null (jsfiddle)

Why does it happen? If I use 'onclick2' in window, it returns false!

I have read Detecting support for a given JavaScript event?, but I would prefer to use something simpler if possible.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • "IE8 in IE7 mode": could the mode functionality be including more recent developments as well, on top of behaving like IE7? – mavili Aug 27 '13 at 20:56
  • @mavili I have no idea, but `hashchange` event doesn't work on IE8 in IE7 mode – Oriol Aug 27 '13 at 21:08

2 Answers2

1

It seems 'onhashchange' in window was true because I was using IE8 in IE7 mode instead of real IE7

But I have just tried it with IETester and 'onhashchange' in window gives false.

Now, I wonder why does IE 8 have IE7 mode if it can't emulate IE7 very well

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • The point is that it emulates the older version *well enough*. Compatibility View was never perfect (nor was it designed to be) and the IE team is continuing to move away from that technology with IE11. – EricLaw Aug 27 '13 at 22:51
-2

I'm not sure why IE is giving you that, but why not just test for true

if(("onhashchange" in window) === true){
  /* Supported */
}else{
  /* Backup */
}
Will
  • 2,604
  • 2
  • 18
  • 14