setAttributeNS (W3C DOM Core method)
document.documentElement.setAttributeNS ('http://www.w3.org/XML/1998/namespace', 'xml:lang', 'en-au-tas');
The example above sets an
xml:lang attribute in the
XML namespace with the value
en-au-tas, and adds it to
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">
Set a new attribute with the specified local name, namespace URI and value to this element.
When setting a value, the value
is not parsed, so any entity references or other markup will be treated as
literal text. To create an attribute containing entities the specification
suggests to create an
Attr node with appropriate
nodes as children, then add it to an element using
setAttributeNode, however in practise this rarely works (see
Attr for details).
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
Attr for details).
Internet Explorer doesn't implement this method (it returns undefined).
Opera 9.0 in pure XML (but not XHTML mode2) parses entities when setting the value of an attribute.
An element may contain two
attributes which have the same local name but different namespaces (for
<a> element may contain both an
href attribute in no namespace, and an
html:href attribute in the XHTML namespace).
Although these are different attributes, Opera
will consider the
html:href attribute to be an
href attribute if an existing
href attribute is not already defined (assuming
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 attributes 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 mode3 behave exactly the same as they do in XHTML mode.
There are no comments yet.