- The Environment: Scripts in the Browser
- The Browser API
- The Document Object Model
The Document Object Model
While the capability to manipulate these browser elements represented a big improvement over pure HTML, developers soon began clamoring for more. Client-side scripting had the potential to be much more powerful and versatile if scripts could manipulate the document itself. This need was met by a document API, a way to control the elements of the HTML document.
Not surprisingly, each of the major browser vendors that implemented a document API did it in its own proprietary way. However, this API has since been standardized, and the W3C continues to work on a specification for what is termed the document object model (DOM). The DOM is a platform-independent and language-neutral standard API for accessing and manipulating the structure of the HTML document using scripts. Modern versions of Web browsers contain a DOM host implementationsoftware that makes the parsed document available to scripts via the W3C standard object model.
As is the case with other technologies that this series has discussed, the primary motivations for following the W3C standard are that it allows developers to write applications that should work correctly on all browsers and platforms that fully support the standard, and that it increases likelihood of code reuse.
The DOM vs. the DHTML Object Model
Browser vendors released proprietary APIs that extended the capabilities of client-side scripting as offered by the browser API. For example, Microsoft's DHTML Object Model is a proprietary API for manipulating the elements of HTML documents in Internet Explorer using HTML, CSS, and client-side scripting.
The DOM, on the other hand, is a formal standard documented in a W3C recommendation. Both APIs are effective at exposing the elements of HTML documents to manipulation by scripts, and there is considerable overlap between the DOM and the DHTML object model. This is not by accident. The DOM represents the evolution of client-side APIs from the earlier proprietary models and is designed to be backward-compatible with the DHTML object model.
The DOM is preferred because it is platform- and language-neutral, and because it works with both HTML and XML. The DOM Level 1 recommendation was finalized in late 1998, followed by the DOM Level 2 recommendation in late 2000. The formalization of the DOM Level 3 standard is in process, with the first public working drafts released at the time of this writing.
Objects
Because these APIs are exposed through object models, client-side scripting with the browser API and the DOM provides many developers with an introduction to object-oriented programming (OOP). The object model defines what data and functionality are exposed and how programmers can manipulate them. For example, the DOM defines what attributes are associated with each element in a Web page, as well as what methods are available and the syntax for calling them.
OOP is a programming paradigm based on abstractionextracting the essential characteristics of something while hiding unessential detail. Central to OOP is the concept of separating how code is implemented from how it is used. Phrased more formally, this is the encapsulation of code and data behind a well-defined interface.
Object-oriented code is organized into discrete data structures called objects. Objects combine both the data in the data structure and the functionality that can be used on that data. Programmers who need to manipulate these are limited to a set of properties and methods that the designer of the object determined when he created the object. These are the public properties and methods of the object. This set of properties and methods is often called the object's interface. For example, the W3C DOM Level 1 Core Specification defines the interface for the Document object to have the following properties and methods:
Properties:
- doctype
- implementation
- documentElement
Methods:
- createElement
- createDocumentFragment
- createTextNode
- createComment
- createCDATASection
- createProcessingInstruction
- createAttribute
- createEntityReference
- getElementByTagName
As is the case with other types of data structures, developers use objects in a program by creating a variable whose type corresponds to the type of object. These variables are then manipulated through their sets of public properties and methods. Most languages use a similar syntax convention, called dot notation, for using object variables. In dot notation, the name of the property or method being used is separated from the variable name using a period, or "dot." For example, the following line of code assigns the name of the first element in the oList object to a variable named sName:
var sName = oList.childNodes(1).nodeName;
The identifier nodeName is the name of a property of the object that is the first element in the list named childNodes. And childNodes is itself an object and is exposed as a property of the oList object.
Elements and IDs
To manipulate document elements using the DOM, scripts must be capable of uniquely identifying and addressing the individual elements of the HTML document. The id attribute is used to identify document elements in the DOM. Document elements referenced by scripts must have been assigned an id value that is unique in the document.
For example, Listing 4 illustrates a Web page that uses a single script to convert the contents of specified text boxes to proper case when the cursor leaves them.
Listing 4: Identifying Document Elements by ID
<html> <head> <title></title> <script language="JavaScript"> <!-- /* --- Change the Value of an Element to Proper Case --- */ function makeProper(vElementId) { var sText = document.all[vElementId].value; var sChar = sText.substring(0,1).toUpperCase(); var sRight = sText.substring(1, sText.length); document.all[vElementId].value = sChar + sRight; } --> </script> </head> <body> First Name: <input id="txtFirstName" type="Text" size="20" onBlur="makeProper(this.id);"><br> Last Name: <input id="txtLastName" type="Text" size="20" onBlur="makeProper(this.id);"><br> Address: <input id="txtAddress" type="Text" size="40"><br> </body> </html>
The onBlur event is fired when the focus switches away from an element. The script works by directly changing the first character of the document element with the id that corresponds to the one specified as an argument to the function.
This article introduced the major components of client-side scripting and their roles in a dynamic Web application. The next article in this series focuses on the language choices available for client-side scripting.