No, this is generally not a safe test, although it may be OK for this particular event since it is relatively new. There is no guarantee about how host objects (such as window
) will respond to the in
operator and some browsers, including old versions Firefox, do not return true
for event handler properties. The following article has details:
http://perfectionkills.com/detecting-event-support-without-browser-sniffing/
It seems Firefox added support for detecting event support using the in
operator in version 9 while onhashchange
was added in 3.6, so unless there was special behaviour for onhashchange
, your test will give false negatives in Firefox 3.6 - 8.0.
UPDATE
I've now tested in Firefox 3.6 and it seems onhashchange
is a special case because your test works: 'onhashchange' in window
returns true, while 'onload' in window
, for example, does not. It looks like your test may be OK, although I would still recommend testing all your target browsers carefully.