When should I observe javascript events on window vs. document vs. document.body?

Posted by brahn on Stack Overflow See other posts from Stack Overflow or by brahn
Published on 2010-04-16T19:24:48Z Indexed on 2010/04/16 19:43 UTC
Read the original article Hit count: 341

I'm using prototype.js for my web app, and I have everything running on chrome, safari, and firefox. I am now working on IE8 compatibility.

As I've been debugging in IE, I've noticed that there are javascript events for which I have previously set an observer on the window, e.g.

Event.observe(window, eventType, function () {...});

(where eventType might be "dom:loaded", "keypress", etc.) and it works just fine in Chrome/Safari/Firefox. However, in IE the observer never fires.

In at least some cases I could get this to work on IE by instead placing the observer on something other than window, e.g. document (in the case of "dom:loaded") or document.body (in the case of "keypress"). However, this is all trial-and-error.

Is there some more systematic way to determine where to place these observers such that the results will be cross-browser compatible?

Thanks!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about javascript-events