|A page in the|
|Statements · Control Flow Statements · Comments · Objects · Functions · Style|
|Scoping · Inheritance · DOM|
In every use, the order of execution is very simple; it begins with the first line of the first file and ends with the last.
Scripts may be placed in either "external" files, "internal" HTML tags, or "inline" HTML attributes. External files are widely accepted as the best among these because:
- They are easier to read (because the brain doesn't have to switch "modes").
- The browser can reuse them (because they are addressable separate from the HTML).
However, situations do exist where the others are appropriate. For example, if something needs to happen directly after a certain image loads, an external or internal script is insufficient. And external scripts are rather inconvenient on programming-oriented message boards, so the recipients of internal scripts are expected to externalize them.
Scripts may be linked from either the
body or (more commonly) the
head. Here is index.html:
You may have noticed that this
Why the same
script tag doesn't work in the
head is left as an exercise for the reader.
<html> <body onload="document.write('Hello, world!');"> </body> </html>
That could almost as easily be written with an
onclick handler, which is more accessible.