Re: Dynamic select lists using optgroups again - without frameworks
- From: RobG <rgqld@xxxxxxxxxxxx>
- Date: Sun, 20 Jan 2008 13:20:47 -0800 (PST)
On Jan 21, 1:03 am, Pascal Ehlert <pascal.ehl...@xxxxxxxxxx> wrote:
Thomas 'PointedEars' Lahn wrote:
First, from the existence of a property or rather its value being
convertible to boolean `true' does not follow necessarily that the property
is a method that can be called. Nor does it follow necessarily from the
non-existence of a property or its value being convertible to boolean
`false' that another property would be available, let alone be a method. So
your feature test is not sufficient, and it makes your code error-prone.
Search the archives for `isMethod' for further information.
Second, both methods are _not_ equivalent. There are a number of problems
with assuming they were, and with attachEvent() itself, see
http://www.quirksmode.org/blog/archives/2005/08/addevent_consid.html
That is indeed an interesting link and stuff that I definitely haven't
been aware of.
I didn't even know that Internet Explorer keeps all previously
registered events in memory, even when you refresh the page. That
explains a few interesting problems I stumbled upon.
Therefore, I have recently suggested a (probably not so new) approach to use
the proprietary event handler property (here: onload) in case
addEventListener() is not available, also supporting several event listeners
for an event an object by emulating the built-in event handler's behavior.
(see <news:478CC039.1060209@xxxxxxxxxxxxxx> and
<news:adf28f78-8090-413b-a518-a699d4f2dcad@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
the thread titled "How do I add to an onchange event?", or search for
"_addEventListener" and my name). The aforementioned Web resource also
provides links to workarounds for this problem that other people have devised.
I think I will find time to look at all these solutions this afternoon,
I'm already very curious to see how you have solved it.
Third, `window' refers to a host object that is not formally specified to
implement the EventTarget interface of W3C DOM Level 2 Events. If you would
try to define the listener for the load event with scripting, you would
therefore assign a Function object reference to the `onload' property of the
Window object instead.
And finally, as I said before, "Unobtrusive JavaScript", which is what you
are still following with this, is a misguided approach devised by people who
don't know better. Despite the explanation a paragraph above, you really
don't want to use methods or proprietary properties of equally proprietary
Window objects to define what should happen when the document has been
loaded. Use the fully standards compliant and widely (if not even
universally) supported approach instead:
<body onload="initDynamicSelects()">
...
</body>
You will observe that you also don't need any DOM branching for this. So
not only this is far more more efficient than the former, but also you are
not forced to rely on any specific DOM implementation (and work around the
quirks it might have).
That's indeed nicer than all those strange dom:ready solutions provided
by numerous frameworks and scripts, but unfortunately I haven't decided
to use "unobstrusive" JavaScript because of idealism, but because I
wanted to add and remove certain behaviour just by loading the files or
not.
It has been mentioned here a number of times that you can achieve the
same effect as a "dom ready" function by inserting all your scripts
just before the closing </body> tab, then calling your initialising
function at the bottom of the last script element, or as the only code
in the last script element.
The DOM will be ready and you don't need to use window.onload (or
document.body or whatever) or DOM ready and it has the pleasant side
effect of loading the page faster.
--
Rob
.
- Follow-Ups:
- Re: Dynamic select lists using optgroups again - without frameworks
- From: Thomas 'PointedEars' Lahn
- Re: Dynamic select lists using optgroups again - without frameworks
- References:
- Dynamic select lists using optgroups again - without frameworks
- From: Pascal Ehlert
- Re: Dynamic select lists using optgroups again - without frameworks
- From: Pascal Ehlert
- Re: Dynamic select lists using optgroups again - without frameworks
- From: Thomas 'PointedEars' Lahn
- Re: Dynamic select lists using optgroups again - without frameworks
- From: Pascal Ehlert
- Dynamic select lists using optgroups again - without frameworks
- Prev by Date: Re: Best method to access an external XML file (ie. http://www.w3schools.com/xml/note.xml) using JS
- Next by Date: Re: Dynamic select lists using optgroups again - without frameworks
- Previous by thread: Re: Dynamic select lists using optgroups again - without frameworks
- Next by thread: Re: Dynamic select lists using optgroups again - without frameworks
- Index(es):
Relevant Pages
|