Hans-Georg Michna said:
Jorge said:
<html>
<head>
<script>
setTimeout(function f1 () {
alert(".html parsing done.");
}, 0);
</script>
</head>
<body>
</body>
</html>
Is it safe to assume that f1 will be run, and that it won't
run until the parsing has ended ?
[...]
it is safe to assume that f1 will be run, but if by "parsing"
you mean "rendering to screen", then no, it will usually run
before the parsing has ended, provided the web page is a bit
bigger than the empty example above.
In all browsers I know the rendering to screen is totally
invisible to the JavaScript subsystem, and there is no direct
way to find out what the browser's rendering engine is doing or
has done. [...]
However, you have evaded the question. Parsing (tokenizing the source code)
must happen before rendering (painting a graphical representation of tokens
that have or generate a graphical representation).
Insofar it stands to reason and can be shown that a `script' element's
content is going to be executed when the `script' element and its previous
siblings have been parsed (but not necessarily rendered, or the
corresponding DOM objects created). So it is possible to determine when
parsing has (nearly) finished; for example, code in a `script' element that
is the last child of the `body' element (ignoring white-space text nodes)
will execute then and not before. (However, with the W3C DOM the `load'
event appears to be best suited to determine that moment more exactly.)
Therefore, if `f1' is called at all (the markup is not Valid, setTimeout()
is not defined a built-in method of the Global Object, the Function instance
reference may not be supported, 0 may not be supported for the second
argument), it is going to be called almost immediately after the `script'
element has been parsed (as 0 would be the approximate number of
milliseconds after the setTimeout() call after which `f1' would be called),
and the correct answer to the OP's question must be "no" (it is not safe to
assume [the opposite]). After all, the `head' element is parsed before the
`body' element, and this `script' element is a child of the `head' element.
But, as you say correctly, in a standard-security environment it is not
possible *in general* to determine with scripting when *rendering* has
finished; for example, the `load' event specification does not define if all
referred image resources have been downloaded and rendered when the event
occurs. There are special cases, though, where it can be determined with
scripting whether rendering was finished.
PointedEars