hasAttributeNS (W3C DOM Core method)

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

Example

var hasid = element.hasAttributeNS('http://www.w3.org/1999/xhtml', 'id');

The example above tests whether an element has an attribute in the HTML namespace called id, and saves the result to the hasid variable.

So if the element in question were this XHTML:

<div class="fools" html:id="nincompoop">

Then the hasid variable would have the boolean value true. But attributes do not inherit a namespace from their owning element, so if the XHTML were simply this:

<div class="fools" id="nincompoop">

Then the hasid variable would have the boolean value false, because the id attribute is in no namespace. Likewise, if the attribute were not there at all:

<div class="fools">

Then the hasid variable would also have the boolean value false.

Arguments

namespace (DOMString) required

The namespaceURI of the attribute to look for. A value of null means no namespace.

name (DOMString) required

The localName of the attribute to look for.

Description

Whether this element has an attribute with the specified local name and namespace URI, or has a specific default value for that attribute in this document type.

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.

Return value

Returns true if an attribute with the specified local name and namespace URI is defined for this element, or has a specific default value in this document type, otherwise false

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

In Opera 9.0, attributes 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.

In Safari 1.3 and 2 attributes inherit a namespace from their owning element, therefore this method matches attributes with no explicit namespace if the namespace argument matches the default. They also treat an xml:lang attribute as a lang attribute, and therefore match it only if the namespace argument is http://www.w3.org/1999/xhtml (even when the document isn't any flavour of HTML). Other XML attributes which have no HTML analog (such as xml:space) are matched with their namespace correctly, but are also matched if the namespace argument is 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 attributes in no namespace if the namespace argument is null or an empty string, or (in the case of Opera 9.0) attributes 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 attributes in no namespace if the namespace argument is null or an empty string; Safari 3 only matches explicitly defined attributes in no namespace if the namespace argument is null; Safari 1.3 and 2 only match explicitly defined attributes 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