Chapter 15. Scripting Documents

Client-side JavaScript exists to turn static HTML documents into interactive web applications. Scripting the content of web pages is the raison d'être of JavaScript. This chapter—the most important in Part II —explains how to do this.

Every web browser window (or frame) displays an HTML document. The Window object that represents that window has a document property that refers to a Document object. This Document object is the subject of this chapter, which begins by studying properties and methods of the Document object itself. These are interesting, but they are only the beginning.

More interesting than the Document object itself are the objects that represent the content of the document. HTML documents can contain text, images, hyperlinks, form elements, and so on. JavaScript code can access and manipulate the objects that represent each document element. Being able to directly access the objects that represent the content of a document is very powerful, but this is also where things start to get complicated.

A Document Object Model, or DOM, is an API that defines how to access the objects that compose a document. The W3C defines a standard DOM that is reasonably well supported in all modern web browsers. Unfortunately, this has not always been the case. The history of client-side JavaScript programming is really the history of the evolution (sometimes in incompatible ways) of the DOM. In the early days of the Web, Netscape was the leading ...

Get JavaScript: The Definitive Guide, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.