Thanks a lot David. Now we're getting somewhere
Possibly.
The body is
definitely available. So I've now got my list of whether to use window
or document.
Use window.* for resize/scroll events.
I don't think you can go wrong there. But realize this is not a
standard. You need to detect whether window.addEventListener exists
at all.
Use document.* for all mouse events and load/unload.
No. Use the body for load/unload. And in fact, don't use unload at
all as it breaks fast history navigation (and there's rarely a good
reason to use it anyway).
It's part of something for a corporate website, there's nobody showing
up with IE4 or Netscape 6.
Hmmm. Is there nobody showing up with Netscape 6 because nobody is
using that browser or because the site breaks in that browser?
Clearly nobody (sane) uses Netscape 6 these days, but do you see the
problem with such (commonly expressed) assumptions?
And if you follow my advice, at least as far as attaching these
listeners goes, you won't have any trouble with those browsers anyway.
It's basically just the most recent few
versions of IE, FF, Chrome, Safari and Opera.
What is? Are you reading from usage statistics? See above.
Not that I'm doing
useragent sniffing or anything,
Good.
I'm just saying what my test range is.
The last few versions of the above browsers is not necessarily a bad
range. Compared to most Web (and even library) developers it is
admirable. Just be careful of reading too much into positive
results. You have to have some understanding of each and every line
of code to be reasonably sure that it will work in the myriad browsers/
configurations that you did not test.
I'll be doing feature testing.
Good. In many cases feature detection is all that is needed. Feature
testing comes in to play when features exist but are known to be buggy
in some cases (this is where the libraries typically regress back to
browser sniffing). Taken to an ultra-paranoid extreme you could
assume this about all features. But of course, not all features are
easily testable. It's an imperfect world, but with experience and
good practice, you can approach perfection rather than flirting with
disaster.
The problem I found is that feature
testing didn't tell me whether I should prefer window or document
because both had the add/remove EventListener (or attach/detach Event
for IE).
This is where experience (and standards) come into play. There is no
standard for window.addEventListener and there are (or at least were)
browsers out there that implement document.addEventLister but not
window.addEventListener. So you should avoid the window version
whenever possible and detect (not test) it before use.
So what if window.addEventListener is missing and you need to listen
for scroll/resize? I would use document.documentElement myself. The
only reason I would not use document.documentElement (or document) for
scroll/resize in the first place is for backward compatibility (ISTM
that some very old browsers and possibly off-brands available today
failed to support scroll/resize listeners on the document object, but
I could be thinking of DOM0).