setNamedItemNS (W3C DOM Core method)
| Version | Depr. | Static |
|---|---|---|
| DOM2 | No | No |
| IE7 | FF1.5+ | SA1.3+ | OP9+ |
|---|---|---|---|
| None | Buggy | Buggy | Buggy |
- Returns
Node- Throws
WRONG_DOCUMENT_ERR,NO_MODIFICATION_ALLOWED_ERR,INUSE_ATTRIBUTE_ERR
Example
var attrs = document.documentElement.attributes;
var lang = document.createAttributeNS
('http://www.w3.org/XML/1998/namespace', 'lang');
lang.nodeValue = 'en-au-tas';
attrs.setNamedItemNS(lang);
The example above saves the attributes collection of the documentElement, then creates a new attribute
node in the XML namespace, with the name lang and the
value en-au-tas, and assigns it to that collection.
If a node with that local name and namespace already existed in the collection then it will be overwritten with the new value; otherwise a new item will be added, and therefore a new attribute node will apply to the original element.
Arguments
- node (
Node) required The node to add to the map. The node can subsequently be retrieved using its
localNameandnamespaceURI.
Description
Add a specified namespaced
node to a NamedNodeMap.
If a node with that
local name and namespace URI already exists in the map, it will be
replaced. For certain kinds of node — where all instances have the same
name (such as #text or #comment) —
it's not possible to store more than one instance of such a node within a
single node map, because the names would clash. The DOM specification
considers this preferable to implementing aliases.
The useage and
behavior of this method is identical to setNamedItem
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.
Return value
If the new node replaces an
existing node, the previously existing 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 | Buggy | Buggy | Buggy |
Internet Explorer doesn't implement this method (it returns undefined).
In Safari
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); conversely
Opera only understands an empty string
to mean no namespace, it doesn't understand null;
Firefox understands null but
will throw a DOMException over an empty string
(code 14: NAMESPACE_ERR).
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.