Scripting in the Browser - Part 1 | WebReference

Scripting in the Browser - Part 1


Scripting in the Browser - Part 1

By Sas Jacobs

Excerpted from Beginning XML with DOM and Ajax: From Novice to Professional, published by Apress, Inc. © 2006

JavaScript provides great flexibility for working with client-side XML. In this chapter, I'll show you how to use JavaScript to work with XML content. The chapter starts by looking at the World Wide Web Consortium (W3C) XML DOM and then shows how to use it with JavaScript to manipulate XML documents.

I'll examine some of the key DOM interfaces before looking at the differences between Internet Explorer (IE) and Mozilla. You'll see one approach to managing these differences using a wrapper library, and you'll finish the chapter by applying what you've learned. During the chapter, you'll learn how to work with XML data dynamically and request content without server-side processing.

I tested the examples in Firefox 1.5 and IE 6.0. You can download the code samples from the Source Code area of the Apress web site.

Let's start by learning more about the XML DOM.


I introduced the W3C DOM earlier in this book. The DOM represents structured documents as an object-oriented model. It creates a tree-like structure of objects that developers can use to target and manipulate parts of the document.

Vendors can implement the DOM interfaces in a language or platform of their choice. This chapter uses JavaScript to manipulate the DOM in IE and Firefox. Both of these browsers provide support for the W3C DOM, but there are some differences between the two.


An interface defines the way that an object interacts with the outside world. Interfaces specify the methods and properties that are available to objects that implement those interfaces. The W3C DOM defines a set of interfaces for accessing XML programmatically. Vendors can implement these interfaces in any language or platform that is appropriate. Both Mozilla and IE implement the W3C DOM. Because they both implement the same interfaces, they share a common set of properties and methods.

The W3C DOM represents an XML document as a tree of nodes. You can see this structure using the dvd.xml document example from Chapter 1:

Figure 8-1 shows this document represented in a tree structure.

The tree contains a hierarchical set of nodes of different types. At the base of the tree, the <library> element has a number of <DVD> elements. Each <DVD> element has <title>, <format>, and <genre> elements.

Let's look at how to interpret this document using DOM interfaces.