Your scripts should not attempt to manipulate objects that have not
yet been created. For example, you can't write a script that
manipulates the contents of an HTML form if the script appears before
the form in the HTML file. Some other, similar rules apply on a
case-by-case basis. For example, there are properties of the Document
object that may be set only from a script in the
<head> section of an HTML document, before
the browser has begun to parse the document content in the
<body> section. Any special rules of this
sort are documented in the reference page for the affected object or
property in the client-side reference.
Since scripts are executed while the HTML document that contains them
is being parsed and displayed, they should not take too long to run.
Because scripts can create dynamic document content with
document.write( ), the HTML parser must stop
parsing the document whenever the JavaScript interpreter is running a
script. An HTML document cannot be fully displayed until all the
scripts it contains have finished executing. If a script performs
some computationally intensive task that takes a long time to run,
the user may become frustrated waiting for the document to be
displayed. Thus, if you need to perform a lot of computation with
JavaScript, you should define a function to do the computation and
invoke that function from an event handler when the user requests it,
rather than doing the computation when the document is first loaded.