04.11.2015 Views

javascript

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

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

Chapter 10: The Document Object Model<br />

In this code, the radio buttons all have a name attribute of “ color ” even though their IDs are different.<br />

The IDs allow the < label > elements to be applied to the radio buttons, and the name attribute assures<br />

that only one of the three values will be sent to the server. These radio buttons can all then be retrieved<br />

using the following line of code:<br />

var radios = document.getElementsByName(“color”);<br />

As with getElementsByTagName() , the getElementsByName() method returns an HTMLCollection .<br />

In this context, however, the namedItem() method always retrieves the first item (since all items have the<br />

same name ).<br />

Special Collections<br />

The document object has several special collections. Each of these collections is an HTMLCollection<br />

object and provides faster access to common parts of the document, as described here:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

document.anchors — Contains all < a > elements with a name attribute in the document.<br />

document.applets — Contains all < applet > elements in the document. This collection is<br />

deprecated, because the < applet > element is no longer recommended for use.<br />

document.forms — Contains all < form > elements in the document. The same as document<br />

.getElementsByTagName( “ form ” ) .<br />

document.images — Contains all < img > elements in the document. The same as document<br />

.getElementsByTagName( “ img ” ) .<br />

document.links — Contains all < a > elements with an href attribute in the document.<br />

These special collections are always available on HTMLDocument objects and, like all HTMLCollection<br />

objects, are constantly updated to match the contents of the current document.<br />

DOM Conformance Detection<br />

Because there are multiple levels as well as multiple parts of the DOM, it became necessary to determine<br />

exactly what parts of the DOM a browser has implemented. The document.implementation property<br />

is an object containing information and functionality tied directly to the browser ’ s implementation of the<br />

DOM. DOM Level 1 specifies only one method on document.implementation , which is<br />

hasFeature() . The hasFeature() method accepts two arguments: the name and version of the DOM<br />

feature to check for. If the browser supports the named feature and version, this method returns true , as<br />

with this example:<br />

var hasXmlDom = document.implementation.hasFeature(“XML”, “1.0”);<br />

275

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

Saved successfully!

Ooh no, something went wrong!