[thelist] Pontification on object and method detection (was: Wanted: the definitive fool-proof mouseover JavaScript)
Joe Crawford
joe at artlung.com
Thu Jun 28 23:46:39 CDT 2001
[ note: I usually don't pontificate
about JavaScript, but this discussion
is intellectually stimulating, so this
is long, but I throw in some Beatles
references for the hell of it.
]
Paola Kathuria wrote:
>> i've still gotta wonder though -- for as long as we've had the hassle of
>> developing for ie3, don't you think this sort of solution would have been
>> discovered already? that to me is an indication that's there's probably
>> something crucial missing (lots of minor version number browsers) from your
>> testing.
>
> Aha, is this the main reason you're not happy with it?
>
> And, no, I don't think that everything's been invented yet
> when it comes to, umm, browser-friendly web techniques.
I'm fascinated that this technique works. I've been doing web crap for a
long time, and am surprised to see a new technique which appears to be
relatively robust.
Still, /intuitively/ if you're going to use an object, you should test for
the object. On this basis, when I want to manipulate layers in a function
called obLaDiObLaDa() (only implemented in Navigator 4.x) I do:
function obLaDiObLaDa() {
if (document.layers) {
// layers go on and on as Netscape 4 does.
} else { // boo hoo no layers }
}
And If I want to use a method which is not available on all browsers which
do JavaScript, you would check for that: it looks very similar:
function backInTheUssr() {
if (window.RegExp) {
// do clever stuff with regular expressions
} else { // regexp's a no go }
}
And If I want to be all fancy, and use a proper newfangled DOM I need to
check for getElementById - and do something else if the browser doesn't
understand it.
function dearPrudence() {
if (document.getElementById) {
// dom you come out to play
} else { // no dom today }
}
Now, this kind of object or method detection has been best practice for
several years. Quite naturally, when you want to play with the
document.images array - as in a mouseover, the orthodox way to do it is
with:
function imSoTired() {
if (document.images) {
// images haven't slept a wink
} else { // no document images, how sad! }
}
This is the whole basis of object detection (in fact, a google search for
"object detection JavaScript" yields many many many, and MANY references.
http://www.google.com/search?q=object+detection+javascript
So to see another approach is, for me, quite novel.
But the problem I have, is that it is so very /tied/ to a peculiarity of how
the <script> tag works. I don't have confidence that new browsers will
implement their scripting engines in the same way. But my gut tells me
(based on much experience!) that a browser detect based on document.images
will continue to work in new browsers which handle a document.images object.
I initially coded http://www.artlung.com/archive/2/home.html back in 1997,
and the mouseovers still work. They worked in Netscape 3. They didn't make
IE3 break. They worked when Netscape 4 came out. They worked when I
discovered Opera. They worked in WebTV. They work on Windows, they work on
Linux, they work on Macs. There is a proven track record with the formula
Jeff is talking about that is missing from your new approach.
And I'll add that these browsers which I have handy execute your script
properly:
Netscape 6.1pr1/Mac
MSIE5/Mac
Opera 5.0tp3.298/Mac
Again, it's interesting that this technique is working, but I instinctively
don't trust it. It just seems logical that one would test for an object
before using it - rather than making blank functions for older browsers,
then making the real function for newer browsers. object testing seems the
more compact technique, logically.
I suppose it's like checking for my keys before I go walking out to my car.
Or like checking my wallet for taking my CD purchases to the counter. Check
for the object, then use the object.
I welcome spirited debate or clarifications on this topic.
- Joe
--
........... Joe Crawford : thinking and design about the web
.... enigmatic narcissism and miscellany : http://artlung.com
.... community instigator : http://WebSanDiego.org
.... San Diego, California, USA .....................AAAFNRAA
More information about the thelist
mailing list