RobG wrote:
You can use DOM methods (including getElementById) in scripts placed
immediately after the nodes they wish to access. You don't have to
wait for the entire document to load - the document exists (at least)
from the moment the browser starts to load the content.
That was a taboo for many years, because placing <scriptright after
the element tag insured only one thing: that the relevant chunk of HTML
source will be *read* by parser before script execution. Whether it
will be properly *parsed* or not to the moment of the script execution:
that depended on the parser algorithms and speed.
Searching c.l.j. for instance will give a good number of "misterious"
cases then blocks
HTML Element
Script handling HTML Element above
failed to work for non-IE browsers or failed to work for the same
browser depending on the document complexity/structure.
In this aspect I see that all UA's I can get on hold at the moment (IE6
SP2, Firefox 1.5.0.6, Opera 8.54) did adopt the IE's mechanics so DOM
is available before finalized.
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</head>
<body>
<p id="output"></p>
<script type="text/javascript">
try {
document.getElementById('output').innerHTML = 'foobar';
window.alert(document.getElementsByTagName('P').le ngth); // 1
}
catch(e) {
window.alert(e.message);
}
</script>
<p id="p01"Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aliquam ultrices,
augue ac tincidunt porta, magna sem porta sem, vel ullamcorper libero
metus
ut nunc. Pellentesque feugiat elementum nibh. Phasellus eget enim at
ipsum
facilisis auctor. Quisque nec dui. In ligula. Duis quis pede.
Maecenas venenatis
tellus vitae arcu. Pellentesque odio mauris, fermentum quis, interdum
ut,
blandit hendrerit, massa. Duis ut erat. Donec nec arcu elementum
lacus vehicula
tempus. Praesent vitae velit vel orci ultricies varius. In in eros in
massa
interdum accumsan. Cras tincidunt. Quisque consequat. Duis tincidunt
metus
sit amet libero. Phasellus non massa sit amet diam pulvinar
imperdiet. Aenean
et neque. Nunc luctus.
<p id="p02">Nulla in erat. Cum sociis natoque penatibus et magnis dis
parturient montes,
nascetur ridiculus mus. Aenean eros massa, condimentum a, egestas
quis, tempus
et, est. Aenean eu leo. Vivamus nibh eros, nonummy sed, sollicitudin
at, porta
eget, purus. Aenean et ipsum. Nulla dictum magna id tellus elementum
facilisis.
Suspendisse potenti. Aliquam erat augue, hendrerit cursus, laoreet
vel, egestas
at, ligula. Nullam viverra nibh et purus. Quisque ornare vestibulum
tellus.
Fusce congue. Cras venenatis viverra nunc. Aenean mattis tellus in
nibh. Aenean
adipiscing. Fusce dictum, turpis facilisis tempus tempor, risus nunc
suscipit
magna, vitae iaculis arcu magna sed nunc. Suspendisse venenatis massa
hendrerit
urna. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
</body>
</html>
In either case it seems as a non-documented feature as all references
still state that "DOM is not available until the document is loaded".
Thanks for pointing out.