getAttributeNodeNS (W3C DOM Core method)

Spec
Version Depr. Static
DOM2 No No
Browser support (more…)
IE7 FF1.5+ SA3+ OP9+
None Buggy Full Buggy
Returns
Attr

Example

var langnode = document.documentElement.getAttributeNodeNS
    ('http://www.w3.org/XML/1998/namespace', 'lang');

The example above retrieves the lang attribute node in the XML namespace attached to the documentElement, and saves it to the variable langnode.

On an RSS page the documentElement is the root rss element, so if it looked like this:

<rss version="2.0" xml:lang="en-gb">

Then the langnode variable would be a reference to an Attr node with the value (and nodeValue) en-gb.

Arguments

namespace (DOMString) required

The namespaceURI of the attribute node to retrieve. A value of null means no namespace.

name (DOMString) required

The localName of the attribute node to retrieve.

Description

Get an Attr node with the specified localName and namespaceURI.

The value of this node can then be retrieved with the value property of Attr, or the more general nodeValue property of Node.

Unlike Element nodes, Attr nodes do not inherit a namespace from the element they're attached to — if an attribute does not have an explicitly defined namespace then it simply has no namespace. See Attr for more details.

Return value

The attribute node; or null if the specified attribute is not defined.

Compatibility

Internet Explorer Firefox Safari Opera
5.5 6.0 7.0 1.5 2.0 3.0 1.3 2.0 3.0 9.0 9.5
None None None Buggy Buggy Buggy Buggy Buggy Full Buggy Buggy

Internet Explorer doesn't implement this method (it returns undefined).

In Opera 9.0, attribute nodes which are not explicitly defined but have a specific default value in this document type are matched if the namespace argument matches the default namespace.

Opera 9.0 returns an interpreted value for the value of a style/html:style attribute node, rather than the literal value. For example, this value in HTML:

style="color:green;font-size:1.5em;position:fakevalue;left:.5ex;"

returns this value property from the node retrieved with getAttributeNodeNS:

color: #008000; font-size: 1.50em; left: 0.50ex;

You can see how several changes are evident — the color value has been interpreted to hexadecimal, the font-size and left values have been normalized with leading and trailing zeros, and the fake value (for which read: value it doesn't understand) has been removed. A space has also been inserted after each colon and semi-colon. (Although Firefox does a similar interpretation for a value retrieved using getAttributeNode, it does not do this for getAttributeNodeNS.)

In Safari 1.3 and 2 attribute nodes inherit a namespace from their owning element, therefore this method matches attribute nodes with no explicit namespace if the namespace argument matches the default.

Additionally, Safari 1.3 cannot see the xml prefix, so an xml:lang attribute node will be treated as a lang attribute node, and matched only if the namespace argument is http://www.w3.org/1999/xhtml (even when the document isn't any flavour of HTML); other XML attribute nodes which have no HTML analog (such as xml:space) will be matched if the namespace argument is null or an empty string. In Safari 2 this is only the case for an xml:lang attribute node — other XML attribute nodes are matched correctly, although they are also still matched if the namespace argument is null or an empty string.

In Firefox and Opera an empty-string namespace is treated the same as a null namespace (ie. it's taken to mean no namespace, when it should be treated as a real namespace URI); only Safari 3 gets this right.

: Behavior in HTML

Since namespaces are an XML construct, it's only reasonable to judge the behavior of this method in terms of XML (either on HTML pages in XHTML mode1, or pure XML). The behavioral variations of browsers in HTML mode2 are documented here for interest and reference, but the support summary table above does not consider this behavior.

All supported browsers in HTML mode behave essentially the same as they do in XHTML mode, except that they don't recognise namespaces at all. (So Opera only matches either explicitly defined attribute nodes in no namespace if the namespace argument is null or an empty string, or (in the case of Opera 9.0) attribute nodes which are not explicitly defined but have a specific default value in this document type if the namespace argument matches the default namespace; Firefox only matches explicitly defined attribute nodes in no namespace if the namespace argument is null or an empty string; Safari 3 only matches explicitly defined attribute nodes in no namespace if the namespace argument is null; Safari 1.3 and 2 only match explicitly defined attribute nodes without a namespace if the namespace argument matches the default.)

Footnotes

1 On XHTML pages served as application/xhtml+xml.

2 On XHTML or HTML pages served as text/html.

User-contributed notes

There are no comments yet.

Related Products