dd wrote:
VK wrote:
Before the page is loaded, the document DOM tree is not completed yet.
That I agree with.
Respectively you cannot manipulate something that doesn't exist yet:
Not quite what you first said. I agree it's not complete, but to say
it doesn't exist is going a bit far. It does exist, it's just not
complete.
It doesn't exist as a finalized scriptable unit where you could apply
DOM methods.
VK wrote:
same way as you cannot set up furniture in a flat before the floor is
build up.
Taking that analogy too far I think. I'd say the floor in the flat is
busy
being built by a few guys going from one side of the room to the other
and I'm just asking them if I can place some furniture "behind them"
onto the piece of floor they've already finished, even if the glue
isn't
dry yet :)
And they kick you out plus giving a nock to the behind: until the level
is finished: for personnel only.
:-)
VK wrote:
There is no difference in this aspect between browsers.
There's a HUGE difference.
Noop. You are cheated by the parser optimisation and look-ahead
mechanics. Try this for instance:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</head>
<body>
<h1>Header</h1>
<script>
try {
var p = document.createElement('p');
p.innerHTML = '<em>Extra paragraph</em>';
document.body.appendChild(p);
}
catch(e) {
document.write('<p style="color:red">' + e.message + '</p>');
}
</script>
<p>Paragraph</p>
</body>
</html>
Both FF and IE will show "Extra paragraph". Did you really managed to
script DOM Tree before it came into existence? No at all. The parser
just gave a micro-delay in script execution to see if it can parse and
finalize DOM during this delay. So your inline script in fact was
implicetly converted into window.onload=addParagraph.
Can you relay on this behavior? I would insistently suggest never do
it. Next time you page will be more complicated or connection slower
and oops: "document.body is null or not an object".
Once again, for any robust solution the mantra is:
before page load event - document.write only
after page load event - DOM methods only
Ommm...
....
before page load event - document.write only
after page load event - DOM methods only
Ommm...
....
Repeat 10 times before go to bed and the life becomes much more easy
and your customers will stay happy, and money will stay in your house.
:-)