23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 7 ■ TRAVERSING AND MODIFYING THE DOM TREE<br />

256<br />

JavaScript object makes DOM language neutral. Internet Explorer, for example, implements DOM with<br />

COM objects, while Firefox, Safari, and Opera implement DOM with JavaScript objects. For this reason,<br />

DOM objects do not behave like JavaScript objects in Internet Explorer, but do so in Firefox, Safari, and<br />

Opera. So, there’s your first taste of Internet Explorer’s “I’ll be on my own side. By myself.” mischief.<br />

■ Note If you are curious as to what COM objects are, visit the following Wikipedia page:<br />

http://en.wikipedia.org/wiki/Component_Object_Model.<br />

DOM is language-neutral, but it is a pretty loose standard, too. Rather than very specifically<br />

documenting classes, DOM tells Internet Explorer, Firefox, Safari, and Opera what to do by way of<br />

interfaces. Interfaces list methods and members that must be implemented together. Just as an interface<br />

is an intentionally vague blueprint for an object, the JavaScript interpreter for Firefox implements DOM<br />

features differently than the JavaScript interpreter for Internet Explorer or Safari does. Moreover, a node<br />

in the DOM tree can implement more than one interface. For example, a tag from your markup is<br />

represented with an Element node in the DOM tree. Those have all the features listed in the Node,<br />

Element, and HTMLElement interfaces, among others.<br />

With this in mind, in order to know what members and methods are available for you to manipulate<br />

a node with, you have to know which interfaces list those features. Although DOM is comprised of<br />

hundreds of interfaces, we will explore just 11 in this chapter. Did I hear a sigh of relief?<br />

Thought so. Anyway, knowing key interface names will also prove invaluable whenever you need to<br />

look up features in a DOM reference, printed or online. For those reasons, I preface DOM methods and<br />

members with their interface names. For example, the method createElement() is listed in the Document<br />

interface, so I refer to it as Document.createElement(). That way, you know it can be invoked only on a<br />

Document node. Also, you know to look under the Document interface in a DOM reference whenever you<br />

want more information.<br />

Alrighty then, these are the 11 DOM interfaces we will explore in this chapter:<br />

Attr<br />

CharacterData<br />

Document<br />

Element<br />

HTMLCollection<br />

HTMLDocument<br />

HTMLElement<br />

NamedNodeMap<br />

Node<br />

NodeList<br />

Text<br />

Is Every Node the Same?<br />

Every node is not the same; there are 12 different kinds of nodes. But as a DOM scripting beginner, and<br />

even later in life when you are a guru, you will really only ever work with four. First, every tag in your<br />

markup is represented with an Element node. Second, tag attributes like id or href are represented with<br />

Attr nodes. Next, not surprisingly, text content is represented with Text nodes. And finally, the whole<br />

enchilada is represented with a Document node—in other words, the root to the DOM tree is a Document<br />

node. Every other kind of node is a descendant of this root node.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!