XML DOM insertNode() method
Definition and Usage
The insertNode() method inserts a node at the beginning of the range.
Syntax:
insertNode(newNode)
parameter
parameter | Description |
---|---|
newNode | the node to be inserted into the document. |
throw
if newNode is an Attr, Document, Entity, or Nortion node, the method will throw an INVALID_NODE_TYPE_ERR code RangeException exception.
Under the following conditions, the method will also throw the DOMException exception as follows:
- HIERARCHY_REQUEST_ERR - The starting point of the range cannot have child nodes, nor can it have child nodes of the specified type, or newNode is an ancestor node of the node (or the node itself).
- NO_MODIFICATION_ALLOWED_ERR - The node containing the starting point of the range (or its ancestor node) is read-only.
- WRONG_DOCUMENT_ERR - newNode is different from the document to which the range belongs.
Description
This method will insert the specified node (and all its child nodes) at the beginning of the document range. When this method returns, the current range will include the newly inserted node. If newNode has already been part of the document, then it will be removed from the current position and reinserted at the starting point of the range. If newNode is DocumentFragment node, then the node inserted is not itself, but its child nodes, inserted in order at the starting point of the range.
If the node containing the starting point of the current range is a Text node, it will be split into two adjacent nodes before the insertion operation occurs. If newNode is a Text node, after it is inserted into the document, it will not merge with any adjacent Text nodes. To merge adjacent nodes, you need to call Node.normalize() method.