[thelist] Pontification on object and method detection (was: Wanted: thedefi

Paola Kathuria paola at limitless.co.uk
Fri Jun 29 13:41:19 CDT 2001


[replies to Peter-Paul Koch and Richard Bennett]

Joe Crawford wrote:
> It's a novel approach to a mouseover.

Peter-Paul Koch wrote:
> I don't agree, I've seen this approach before.

Me too.  I realised that I use it a lot when programming.  If
I'm going to act on a variable that depends on a condition, I
first set the variable to the default or a safe value (e.g.,
empty string, 0, whatever).  A conditional then surrounds code
which sets the value to its "proper" value.  If the condition
fails, there's still a usable value for the variable (causing
a default or no action but not a runtime error).

I'm sure this fail-safe approach is well-known by programmers
- I wouldn't be surprised if there was a term it.

> Add the following browsers to the list (all Win98): [...]

Thanks.

[from an earlier message]
> I wouldn't rely on something so unimportant as the version number.
> [...]
> The version number simply does not have a direct relation with
> the supported objects and methods, while object/method detection
> does.

It may not now (post JS 1.1) but it was, I believe, meant to.
It was supposed to be how you tested for language features.

The rest of this reply is to Richard Bennett's message - the
upshot is replacement functions but still using the fail-safe
pre-script approach.  If you don't want to read the reasoning
below, they're at:
http://www.limitless.co.uk/~paola/tmp/thelist/test3.html

Richard Bennett wrote:
> the code as it stands now will fail miserably in NS4, if the
> links on the page are nested in a <div> (as most menus of
> mouse-over links are), as NS4 creates a new document object
> for each level of nesting

Ah.  I use NS4 but I tend not to use divs.  Of course, many
people do and so it looks like these functions aren't suitable
for people who rely on CSS.

> So suddenly the whole idea is less robust than it seemed to start off with.
> What we can do, is pass the image object to the img_inact() function,

This causes an error in IE 3, though (the minor browser which 
started this whole thread). See
http://www.limitless.co.uk/~paola/tmp/thelist/rb.html
Have I implemented it correctly?

> If you do not support javascript 1.0 browsers, you can condense it to this:
> 
> <a href="./" onMouseover="libr.src=libron.src"
> onMouseout="libr.src=libroff.src"><img src="b-libr0.gif"
> name="libr" border="0" alt="" width="60" height="50"></a>

This is glorious but I was looking for a solution that wouldn't
produce JS errors on any browser that claims to know JS.

> That looks kind of familiar, nice and simple, then of course you could loop
> your preload code

My preload code is actually generated in a loop at the server.

I have now came up with an alternative script which suits me
(and my development environment) but perhaps not most people.
It's a mock-up of a script which would be generated at the
server (which is handed a list of 4-letter button ids and does
all the expansions).  My reasoning is that JS is flakey enough,
so make it do the minimum neccessary.

It's at http://www.limitless.co.uk/~paola/tmp/thelist/test3.html
With indentation, it's 124 bytes bigger than my original scripts
but less than if it were converted to use object tests.
Besides the use of the first script to replace the testing of
objects, is there anything wrong with it?

Now that I know there's nothing iffy about testing for
document.images, the only purpose of the first script (which
defines empty functions) is to stream-line the main script
when it's JS1.1 (I accept that using this approach for
declarations of 1.2+ will be unreliable).

> I was also wondering what this bit was for: class="nul" I guess
> the one l was a typo, but why specify a null?

Ah, that's because I copied the mark-up from an actual web
page.  The "nul" means "no underline" to me (and I'm aware
that it's similar to null) - because all the buttons will
be combined with an HTML text link.


Paola

p.s. The book I've mentioned a few times that has a chapter
on compatibility techniques is "Javascript: The Definitive Guide
(3rd Edition)" (1998) by David Flanagan, published by O'Reilly.
http://www.amazon.com/exec/obidos/ASIN/1565923928/




More information about the thelist mailing list