A big part of the problem, I think, is that so much of what
you find on the web are hacks, instead of good well-written
code. Most of it was probably written years ago too.
Take for example this:
<html>
<head>
<script>function a (){ alert(t.i.value) }</script>
</head>
<body>
<form name="t"><input name="i" onchange="a()"></form>
</body>
As HTML 4 transitional the only invalid aspects of that mark-up appear
to be the absence of a TITLE element and a missing TYPE attribute in the
SCRIPT element. Neither of those errors would impact upon the behaviour
of the script in any real-world browsers.
Well, it doesn't. Assuming that a named form will be made available as a
named property of the global object is a bad habit aquifer because it is
true of IE browsers. It was never true of a significant proportion of
other browsers. That script never would have worked in a Netscape
browser, for example.
but it's not valid html. If you change <html> to this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
That is seriously disingenuous. Asking any script to survive the
transition from HTML to XHTML is very optimistic, as the XHTML DOM
requires a style of scripting that is very distinct from that required
for an HTML DOM. Asking HTML mark-up to successfully pass through an XML
parser is totally unrealistic.
then suddenly that code wont work.
Given that the original script was aimed at proprietary features of IE's
object model, and IE doesn't support/render XHTML at all, that isn't a
particularly unexpected change, though very few non-trivial scripts
would survive the transition form an HTML DOM to an XHTML DOM anyway.
It wont work because it was
invalid all along -
The invalidity in the original is not a factor in determining whether
that script would work or not.
I'm supposed to use document.getElementById("i"),
There are no elements in the original mark-up that have the ID "i", so
that would not be a useful change to make. The W3C HTML DOM specified
(and very back-compatible) - document.forms - collection is the place to
look-up forms by name (and/or ID, in W3C DOM implementations), and the -
elements - collection of the form is the place to look-up named (and/or
IDed, in W3C DOM implementations) form controls.
So you see, the problem isn't really compatibility.
It's often validity.
You haven't made your point at all. There are plenty of opportunities to
demonstrate invalid mark-up (particularly structurally invalid mark-up)
having a detrimental impact upon the scripted interaction with the
resulting DOM, particularly around the invalid placement of forms within
TABLE elements, but this 'example' is most likely to detract from a
case for the use of valid mark-up.
Richard.