setAttributeNodeNS (W3C DOM Core method)
| Version | Depr. | Static |
|---|---|---|
| DOM2 | No | No |
| IE7 | FF1.5+ | SA3+ | OP9+ |
|---|---|---|---|
| None | Buggy | Full | Buggy |
- Returns
Node- Throws
WRONG_DOCUMENT_ERR,NO_MODIFICATION_ALLOWED_ERR,INUSE_ATTRIBUTE_ERR
Example
var attr = document.createAttributeNS
('http://www.w3.org/XML/1998/namespace', 'xml:lang');
attr.nodeValue = 'en-au-tas';
document.documentElement.setAttributeNodeNS(attr);
The example
above creates an xml:lang attribute in the XML
namespace, assigns it the nodeValue
en-au-tas, and then adds it to the documentElement.
So if the element in question were this HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
Then the operation above would result in this:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-au-tas">
Arguments
- attr (
Attr) required The attribute node to add. The node can subsequently be retrieved using its
localNameandnamespaceURI.
Description
Add a namespaced attribute node to this element.
If an attribute already exists with the specified local name and namespace URI, the existing node is replaced.
The usage and behavior of this method is identical to setAttributeNode except that the local name and namespace
URI are both taken into account when determining whether an existing
attribute should be replaced, rather than just the name.
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 details).
Return value
If the new attribute replaces
an existing attribute with the same local name and namespace URI, the
previously existing attribute node is returned ; otherwise
null is returned.
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).
Opera 9.0 in pure XML (but not XHTML mode1) parses entities when setting the value of an attribute node.
In Safari 1.3
and 2 it's not possible to set the value of a created
attribute using the value property (the
property is readonly; however using the nodeValue property is supported fine).
An
element may contain two attribute nodes which have the same local name but
different namespaces (for example, an a element may
contain both an href attribute node in no
namespace, and an html:href attribute node in the
XHTML namespace). Although these are different attribute nodes,
Opera will consider the
html:href attribute node to be an
href attribute node if an existing
href attribute node is not already defined
(assuming that the html prefix has been declared and
assigned to the default namespace). This behavior is not limited to XHTML,
but occurs on all XML documents in relation to a prefix declared for the
default namespace. This cannot really be considered a bug, because the DOM
does not define attribute associations (that job is handled by a DTD);
it's more like a convenience feature, though admittedly a very confusing
one!
Safari 1.3 and 2 do the same thing as Opera in relation to XHTML (but not XML generally), however they also inherit a namespace from their owning element, therefore they will also overwrite attribute nodes with no explicit namespace if the namespace argument matches the default.
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.
All supported browsers in HTML mode2 behave exactly the same as they do in XHTML mode.
User-contributed notes
There are no comments yet.
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.