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 11: DOM Levels 2 and 3<br />

When a node uses a namespace prefix, the nodeName is equivalent to prefix + “ : ” + localName .<br />

Consider the following example:<br />

< html xmlns=”http://www.w3.org/1999/xhtml” ><br />

< head ><br />

< title > Example XHTML page < /title ><br />

< /head ><br />

< body ><br />

< s:svg xmlns:s=”http://www.w3.org/2000/svg” version=”1.1”<br />

viewBox=”0 0 100 100” style=”width:100%; height:100%” ><br />

< s:rect x=”0” y=”0” width=”100” height=”100” style=”fill:red” / ><br />

< /s:svg ><br />

< /body ><br />

< /html ><br />

For the < html > element, the localName and tagName is “ html ” , the namespaceURI is “ http://www<br />

.w3.org/1999/xhtml ” , and the prefix is null . For the < s:svg > element, the localName is “ svg ” , the<br />

tagName is “ s:svg ” , the namespaceURI is “ http://www.w3.org/2000/svg ” , and the prefix is “ s ” .<br />

DOM Level 3 goes one step further and introduces the following methods to work with namespaces:<br />

❑<br />

❑<br />

❑<br />

isDefaultNamespace( namespaceURI ) — Returns true when the specified namespaceURI is<br />

the default namespace for the node.<br />

lookupNamespaceURI( prefix ) — Returns the namespace URI for the given prefix .<br />

lookupPrefix( namespaceURI ) — Returns the prefix for the given namespaceURI .<br />

In the previous example, the following code can be executed:<br />

alert(document.body.isDefaultNamespace(“http://www.w3.org/1999/xhtml”); //true<br />

//assume svg contains a reference to < s:svg ><br />

alert(svg.lookupPrefix(“http://www.w3.org/2000/svg”)); //”s”<br />

alert(svg.lookupNamespaceURI(“s”)); //”http://www.w3.org/2000/svg”<br />

These methods are primarily useful when you have a reference to a node without knowing its<br />

relationship to the rest of the document.<br />

Changes to Document<br />

The Document type is changed in DOM Level 2 to include the following namespace - specific methods:<br />

❑<br />

❑<br />

❑<br />

createElementNS( namespaceURI , tagName ) — Creates a new element with the given<br />

tagName as part of the namespace indicated by namespaceURI<br />

createAttributeNS( namespaceURI , attributeName ) — Creates a new attribute node as<br />

part of the namespace indicated by namespaceUR .<br />

getElementsByTagNameNS( namespaceURI , tagName ) — Returns a NodeList of elements<br />

with the given tagName that are also a part of the namespace indicated by namespaceURI<br />

320

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

Saved successfully!

Ooh no, something went wrong!