disappearedng said:
1) the "onload="init();"" part of my body is executed AFTER the entire
HTML has been parsed?
No, only after it has been *rendered* also (with some exceptions).
2) Hence the second print works because init() is called AFTER myDiv has
been created, and when init calls print THEN the javascript parser looks
up the already created myDiv element and provides a non null reference?
Roughly speaking, yes.
What really happens in the majority of cases is: The client-side script
source code is parsed into language tokens according to the (modified)
ECMAScript grammar, and JIT-compiled into byte-code before execution.
The Virtual Machine that interprets (executes) that byte-code program then
calls a method of a DOM object that is an implementation of an interface
(method), as the result of the compilation. That method cannot return a
non-null value (with the meaning of `null' being implementation-dependent)
if the DOM object that represents the (HTML) element (an element object) has
not been registered in the DOM tree yet.
Sometimes element objects are created and registered in the DOM tree when
the corresponding element has been parsed. But since the parse tree of the
markup parser is incomplete until the last element (usually the `html'
element) has been parsed or inferred (because of optional tags), you cannot
be sure that the DOM tree is complete either until the `load' event of the
document (standardized in the W3C DOM Level 2 Events Specification) occurs.
That event can be handled with the intrinsic `onload' event-handler
attribute of the `body' element (among other, less reliable methods), as
standardized in the HTML 4.01 Specification.
PointedEars
P.S.: I selected the wrong menu item again, sorry for the e-mail.