04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11: DOM Levels 2 and 3<br />

❑<br />

❑<br />

removeNamedItemNS( namespaceURI , localName ) — Removes the item from the namespace<br />

represented by namespaceURI and with a name of localName<br />

setNamedItemNS( node ) — Adds node , which should have namespace information already<br />

applied<br />

These methods are rarely used, because attributes are typically accessed directly from an element.<br />

Other Changes<br />

There are some other minor changes made to various parts of the DOM in DOM Level 2 Core. These<br />

changes don ’ t have to do with XML namespaces and are targeted more toward ensuring the robustness<br />

and completeness of the API.<br />

Changes to DocumentType<br />

The DocumentType type adds three new properties: publicId , systemId , and internalSubset . The<br />

first two of these properties represent data that is readily available in a doctype but were inaccessible<br />

using DOM Level 1. Consider the following HTML doctype:<br />

< !DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”<br />

“http://www.w3.org/TR/html4/strict.dtd” ><br />

In this doctype, the publicId is “ - //W3C//DTD HTML 4.01//EN ” and the systemId is “ http://www<br />

.w3.org/TR/html4/strict.dtd ” . Browsers that support DOM Level 2 should be able to run the<br />

following JavaScript code:<br />

alert(document.doctype.publicId);<br />

alert(document.doctype.systemId);<br />

Accessing this information is rarely, if ever, needed in web pages.<br />

The internalSubset property accesses any additional definitions that are included in the doctype, as<br />

shown in the following example:<br />

< !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”<br />

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”<br />

[ < !ELEMENT name (#PCDATA) > ] ><br />

For this code, document.doctype.internalSubset returns “ < !ELEMENT name (#PCDATA) > ” .<br />

Internal subsets are rarely used in HTML and are slightly more common in XML.<br />

Changes to Document<br />

The only new method on Document that is not related to namespaces is importNode() . The purpose of<br />

this method is to take a node from a different document and import it into a new document so that it can<br />

be added into the document structure. Remember, every node has an ownerDocument property that<br />

indicates the document it belongs to. If a method such as appendChild() is called and a node with a<br />

different ownerDocument is passed in, an error will occur. Calling importNode() on a node from<br />

a different document returns a new version of the node that is owned by the appropriate document.<br />

322

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

Saved successfully!

Ooh no, something went wrong!