Visit Mozilla.org

DOM:element.getAttributeNS

From MDC

« Gecko DOM Reference

Contents

[edit] Summary

getAttributeNS returns the string value of the attribute with the specified namespace and name. If the named attribute does not exist, the value returned will either be null or "" (the empty string); see Notes for details.

[edit] Syntax

attrVal = element.getAttributeNS(namespace, name)

[edit] Parameters

  • attrVal is the string value of the specified attribute.
  • namespace is the namespace of the specified attribute.
  • name is the name of the specified attribute.

[edit] Example

var div1 = document.getElementById("div1");
var a = div1.getAttributeNS("www.mozilla.org/ns/specialspace/", 
                            "special-align");
alert(a); // shows the value of align for that div

[edit] Notes

getAttributeNS differs from getAttribute in that it allows you to further specify the requested attribute as being part of a particular namespace, as in the example above, where the attribute is part of the fictional "specialspace" namespace on mozilla.

Essentially all web browsers (Firefox, Internet Explorer, recent versions of Opera, Safari, Konqueror, and iCab, as a non-exhaustive list) return null when the specified attribute does not exist on the specified element. The DOM specification says that the correct return value in this case is actually the empty string, and some DOM implementations implement this behavior. Consequently, you should use hasAttributeNS to check for an attribute's existence prior to calling getAttributeNS if it is possible that the requested attribute does not exist on the specified element.

DOM methods dealing with element's attributes:

Not namespace-aware, most commonly used methods Namespace-aware variants (DOM Level 2) DOM Level 1 methods for dealing with Attr nodes directly (seldom used) DOM Level 2 namespace-aware methods for dealing with Attr nodes directly (seldom used)
setAttribute (DOM 1) setAttributeNS setAttributeNode setAttributeNodeNS
getAttribute (DOM 1) getAttributeNS getAttributeNode getAttributeNodeNS
hasAttribute (DOM 2) hasAttributeNS - -
removeAttribute (DOM 1) removeAttributeNS removeAttributeNode -

[edit] Specification

DOM Level 2 Core: getAttributeNS

[edit] See also