setAttributeNodeNS (W3C DOM Core method)
var attr = document.createAttributeNS ('http://www.w3.org/XML/1998/namespace', 'xml:lang'); attr.nodeValue = 'en-au-tas'; document.documentElement.setAttributeNodeNS(attr);
So if the element in question were this HTML:
Then the operation above would result in this:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-au-tas">
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.
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).
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.
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.
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
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.
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.
There are no comments yet.