Mike: Are there any ru le s such as where to p ut a <script > ele ment referring to
JavaScript code that will execute asynchronously?
Professor: There is a general agreement that you place it inside the head of the doc-
ument. You’ll also come across the advice that <script > elements should be placed
after any <link> elements that refer to external CSS. That is, however, irrelevant for
JavaScript that is executed asynchronously because the style information will already
have been available at the time of JavaScript code execution no matter where the ac-
tual code is include d. Tha t’s because styles are needed if the page is to be rendered
correctly.
Mike: Why is tha t impor ta nt? I mean what ha s Java Script to do with CSS?
Professor: You will soon learn that you can manipulate styles dynamically using
JavaScript, which is called style scripting. If you want to manipulate styles, it is
important that they are available, of course. Tha t’s why CSS should be loaded before
JavaScript executes. But more on this later.
12.4 Scripting Documents
Professor: The mo st fundam ental role of client-side JavaScript is to make static
HTML documents responsive to user and other ac tions and turn them into either dy-
namic documents or web applications.
If yo u use JavaScript in web documents only to a ugment HTML with behavior, then
you get what is usua lly called Dyna m ic HTML or DHTML. In such cases, JavaScript
should only be used to enhance a visitor’s browsing experience. The actual content
that a visitor can read, however, should by no means be dependent on JavaScript.
On the other hand, if you go beyond tha t limitation of merely enhan cing a visitor’s
experience, you get what is called a web application. The HTML5 specification de-
fines a whole lot of objects that allow yo u to perform application-like feats such a s
manipulating graphics or storing and retrieving data. Such sets of objects are called
application programming inte rfa ces or APIs because programmers use them for inter-
acting with different (software and hardware) components. There’s simply too much
of everything, so we’re not going to plunge into details. I’ll just leave you so me clues
at the end of this course as to which directions you can take. They are gather ed in
Appendix B of this book.
It doesn’t matter whether you’re up to developing dynamic doc uments or web appli-
cations—there’s o ne API you w ill constantly work with. It is called the Document
Object Model API, or DOM API. Think of DOM as being an object representation of
the collection of all the HTML elements and strings of text positioned on a web pag e.
By usin g DOM API, the programmer can manipulate or query any ele ment or string
of text on a web page indirectly by manipulating or querying a corre sponding DOM
object.
The fundamenta l object of th e DOM API is the Document object, which represents
whatever is displayed in the browser’s window. Th e Document object is a property
of the Window object and can hence b e accessed a s a global property under the n ame
12.4. Scripting Document s 233