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 />

These methods are used by passing in the namespace URI of the namespace to use (not the namespace<br />

prefix), as shown in the following example.<br />

//create a new SVG element<br />

var svg = document.createElementNS(“http://www.w3.org/2000/svg”,”svg”);<br />

//create new attribute for a random namespace<br />

var att = document.createAttributeNS(“http://www.somewhere.com”, “random”);<br />

//get all XHTML elements<br />

var elems = document.getElementsByTagNameNS(“http://www.w3.org/1999/xhtml”, “*”);<br />

The namespace - specific methods are necessary only when there are two or more namespaces in a given<br />

document.<br />

Changes to Element<br />

The changes to Element in DOM Level 2 Core are mostly related to attributes. The following new<br />

methods are introduced:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

getAttributeNS( namespaceURI , localName ) — Gets the attribute from the namespace<br />

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

getAttributeNodeNS( namespaceURI , localName ) — Gets the attribute node from the<br />

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

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

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

namespaceURI .<br />

hasAttributeNS( namespaceURI , localName ) — Determines if the element has an attribute<br />

from the namespace represented by namespaceURI and with a name of localName . Note: DOM<br />

Level 2 Core also adds a hasAttribute() method for use without namespaces.<br />

removeAttributeNS( namespaceURI , localName ) — Removes the attribute from the<br />

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

setAttributeNS( namespaceURI , qualifiedName , value ) — Sets the attribute from the<br />

namespace represented by namespaceURI and with a name of qualifiedName equal to value.<br />

setAttributeNodeNS( attNode ) — Sets the attribute node from the namespace represented<br />

by namespaceURI .<br />

These methods behave the same as their DOM Level 1 counterparts with the exception of the first<br />

argument, which is always the namespace URI.<br />

Changes to NamedNodeMap<br />

The NamedNodeMap type also introduces the following methods for dealing with namespaces. Since<br />

attributes are represented by a NamedNodeMap , these methods mostly apply to attributes.<br />

❑<br />

getNamedItemNS( namespaceURI , localName ) — Gets the item from the namespace<br />

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

321

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

Saved successfully!

Ooh no, something went wrong!