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

The various browsers' object documentation (e.g. window on MSDN, document on MDC) define which events are supported on the object. You could start there.


(This is not a super-comprehensive answer, but it seems to work out empirically -- so hopefully these rules of thumb will be helpful to others.)

  • In general, register events on document, not window. Webkit and mozilla browsers seem to be happy with either, but IE doesn't respond to most events registered on the window, so you need to use document to work with IE

  • Exception: resize, and events related to loading, unloading, and opening/closing should all be set on the window.

  • Exception to the first exception: dom:loaded must be set on document in IE.

  • Another exception: When detecting keystrokes under Mozilla with find-as-you-type enabled, set your key event observers on the window, not the document. If you do the latter, the find-as-you-type seems to block the event.