importNode (W3C DOM Core method)
var xmldoc = request.responseXML; var node = document.importNode(xmldoc.getElementsByTagName('foo').item(0), true); document.getElementsByTagName('body').item(0).appendChild(node);
The code above refers to the
responseXML of an
XMLHttpRequest, which itself is an an XML document.
The example imports the first
Element node called
foo from that document — including its subtree — and
then appends it to the
body of this document.
importNode must be called on this
document, not the document you're importing from.
//THIS IS WRONG! var node = xmldoc.importNode(xmldoc.getElementsByTagName('foo').item(0), true);
Doing so may cause some version of Safari to crash (see Compatibility notes below).
- node (
The node to import.
- deep (
truethen the subtree underneath the specified node is also imported; if
falsethen only the node itself is imported.
EntityReferencenodes this argument is ignored (see notes above). For
Notationnodes this argument is redundant because these types of node cannot have children.
Imports a node from another
document into this one, optionally including all its children. The
imported node is owned by the importing document (
ownerDocument is equal to
document), and has no parent until it's attached.
The original node is not
altered or removed from the document it came from — this method creates a
copy of the node in this document. The new node has the same
the original node, as well as any namespace information (
namespaceURI) if applicable.
information is also copied as appropriate so that the new node mirrors the
properties of the same type of node in this document (for example, if the
node is an
Element it will now have any default
attributes that apply to that element in this document type).
Here are the specifics of behavior for each type of node:
true. The descendants of the attribute are imported recursively to form the same subtree as the original. The deep argument has no effect on attribute nodes — their children are always included when importing.
If the deep argument is
truethen the document fragment's subtree is imported intact; otherwise an empty
DocumentFragmentnode is created.
These types of node cannot be imported.
Explicitly defined attributes are imported along with the element. Attributes which are not defined but have a default value in the original document are not included, but attributes which have a default value in this document are created and assigned to the imported element. If the deep argument is
truethen the element's subtree is also imported.
Entitynodes can be imported, they cannot be assigned to this document type, because
DocumentTypenodes are readonly. All properties of the original entity are imported (
notationName); if the deep parameter is
truethen the subtree is also imported.
Only the entity reference itself is imported — its descendants are never included, even if the deep argument is
true, because the original document and this document may defined the entity differently. If this document type has a definition for this entity name, its value is assigned.
Notationnodes can be imported, they cannot be assigned to this document type, because
DocumentTypenodes are readonly. All properties of the original notation are imported along with it (
systemId); the deep parameter has no effect because
Notationnodes cannot have children.
The new node in this document
Internet Explorer doesn't implement this method (it returns undefined).
A cross-browser recreation of
be found in the ALA article Cross-Browser Scripting with importNode(), by
Anthony Holdener. It doesn't actually import nodes — it re-creates
them — but the end result is the same for most purposes.
In all supported browsers, event-handling attributes on imported elements are not active, and will need to be re-assigned.
Additionally, Safari 3 in HTML
mode1 won't import
Element nodes from XML that isn't XHTML if the
deep argument is
true, with the same
exception. It does work for XHTML. It also works for other XML if
importNode is called on the document you're importing
from, however ...
In Safari 1.3 and 2, calling
importNode on the document you're importing from
(rather than this document), and with the deep
parameter set to
true, will cause the browser to
1 On XHTML or HTML pages served as
There are no comments yet.