As soo n as we become aware of the environment in which our programs run, it be-
comes of paramount importance to know where to put the code and how the code
is executed. So far we put our examples into <sc ript> e le ments embe dded within
HTML code, which is convenient for learn ing and quick testing, but discouraged in
modern web design. Recall that HTML, CSS, and JavaScript each play their own
individual role inside a web page by separately defining content, presentation, and
behavior. Also recall how you separate CSS code from HT ML by putting it to a sep-
arate file and linking both by mean s of the <link> HTML element. You can separate
JavaScript from HTML in the same way and put it in an external file, which you
connect with HTML using the src attribute of th e <script> element. JavaScript de-
tached this way is also called unobtrusive JavaScript, and for an obvious reason —it
makes HTML code much mor e readable because there are no blocks of JavaScript
code sitting in between the sections of HTML code. This is not the so le advantage
of such a separation, though. For example, if multiple HTML files share the same
JavaScript code, you can maintain only a single copy of the code , rather than having
to edit each o f the HTML files whenever you want to change it.
Mike: If y ou put JavaScript code into an external file and include it by mea ns of a
<script> element, then I suppose that everything else stays the same. I mean the
position of the <script> e le ment doesn’t change, does it?
Professor: If you want to implement the con cept properly, then <s cript> elements
shouldn’t be scattered all over HTML code either. This is a proble m so long as you use
document.write() to write to the browser window. Namely, even if JavaScript code
is placed to an external file, it is still executed synchronously. Th is means that it is
executed exactly when the <script> element referring to it is encountered during the
document parsing. The parser will wait until the code has executed before proceeding
with the parsing of the HTML that follows. If you use any doc ument.write() m eth-
ods, the text produced by those methods will be inser te d into the current location in the
input stream of H TML code that is being parsed. So you cannot m ove JavaScript code
that c ontains any calls to document.write( ) without also changing the lo cation of
the output they pro duce.
Another p roblem with document.write() is that you canno t use it after the docu-
ment parsing process ha s completed. Calling it after the document has closed will
erase the document completely. As soon as you want to design a page that is respon-
sive to user actions, this is not the kind of behavior that you would want. That’s why
document.write() is ra rely, if at all, used in modern web pages.
Maria: I suppose that you will show us alternatives.
Professor: Indeed I will, we’re coming to tha t shortly. But first things first: I need to
tell you about events so we can start executing JavaScript code asynchronously.
12.3 Events
Professor: When the docu ment loads completely and the web page is displayed, the
story is usually not finished. On the contrary, it’s on ly now that interesting things begin
to happen. After the document has been parsed and lo aded, the JavaScript enters its
230 Meeting 12. Using JavaScript to Control the Browser