Jukka said:
It depends on the DTD, and the issue has no impact on the functionality or
appearance of the page.
While it is theoretically possible for such a document to pass Validation by
use of a fitting DOCTYPE declaration, if you use a DOCTYPE declaration that
does not fit a specific subset of combinations of system and public
identifiers, MSHTML and other layout engines will use Compatibility/Quirks
Mode. You do not want that to happen to you, both with regard to rendering
and DOM scripting. Besides, one does not know what is going to happen in
the future with either the target environments, the Web application, or
MSHTML. So this approach should be avoided.
- There's no reason to capitalize the word "valid".
Yes said:
Admittedly, it might be cleaner to do as you suggest, defining the event
handler purely in JavaScript,
.... or, in this case, JScript ...
for two reasons. If your markup otherwise complies to HTML recommendations
by the W3C, you can then use their DTD when you validate your page. And
you avoid the (small) potential risk that some other browser starts
recognizing the currently IE-specific attributes, handling them in
somewhat different ways that you are not prepared to.
Exactly.
While at this, I start wondering whether event attributes (in HTML) should
be generally replaced by JavaScript code that assigns event handlers to
elements. I would expect browsers that do not support the latter way have
lost all practical significance long ago, so is it just by habit that
people use event attributes?
No, there are good reasons for not usually doing that. David has mentioned
some. I would like to emphasize one or two things he indicated, what I have
said often enough here and elsewhere:
If you replace *standards-compliant* intrinsic event-handler attributes with
event-handler properties, you will have to face the fact that the latter are
proprietary and do not need to be supported (in the way one would expect).
You can test for methods of W3C DOM Level 2+ Events and alternatively the
MSHTML DOM, and use those instead if the feature test turns out successful,
but none of those needs to be supported either.
It is also not possible to reliably determine for all event-handler
properties whether they are supported or not: Some DOM implementations let
supported event-handler properties have the `undefined' value until set
instead of `null'. So the otherwise reasonable test
typeof obj.on… != "undefined"
would produce a false positive then. But you are dealing with host objects
here, so anything may happen on assignment. In particular, you could be
trying to augment a host object with a property without knowing it. That is
known to be error-prone, and failure to do so cannot be considered a bug in
the DOM implementation as the ECMAScript Specification explicitly allows it
for host objects.
So not only make the additional feature tests -- if taken seriously, so no
simple type-converting tests -- make the Web application (which I think is a
good umbrella term for describing such documents) less efficient and more
error-prone than it needs to be, but also you are restricting yourself to
only a small subset of known DOM implementations, often needlessly. Neither
happens or is implicitly required with event-handler attributes.
That is not to say that there are not useful applications for doing what you
suggest. (I think I have showed one here.) However, most implementations
of the design pattern simply neglect the aforementioned facts ("it works in
$FAVORITE_BROWSERS, so …") and, more important, the possibility of
interoperable event delegation.
This is most visible with (mis)uses of the `click' event, which does bubble
in all known DOM implementations since the concept was introduced with
Netscape Navigator 3.0 in 1996 CE. Still, there is enough code out there
that inefficiently determines fitting descendants (sometimes *hundreds*) by
use of (a) CSS(-like) query syntax, adds a `click' listener to *each* of
them (one that often uses a closure with a host object on top of that, a
pattern known to leak memory in most MSHTML versions) when it could have
added just *one* listener that makes the distinction to a *common ancestor*
to which the event must bubble up, for the comparably unlikely case that the
user causes the event to be created and dispatched to such a descendant.
HTH
PointedEars