Section 25.214. Node.insertBefore( ): insert a node into the document tree before the specified node


25.214. Node.insertBefore( ): insert a node into the document tree before the specified node

DOM Level 1 Core

25.214.1. Synopsis

 Node insertBefore(Node newChild,                   Node refChild)     throws DOMException; 

25.214.1.1. Arguments

newChild

The node to be inserted into the tree. If it is a DocumentFragment, its children are inserted instead.


refChild

The child of this node before which newChild is to be inserted. If this argument is null, newChild is inserted as the last child of this node.

25.214.1.2. Returns

The node that was inserted.

25.214.1.3. Throws

This method may throw a DOMException with the following code values:


HIERARCHY_REQUEST_ERR

This node does not support children, it does not allow children of the specified type, or newChild is an ancestor of this node (or is this node itself).


WRONG_DOCUMENT_ERR

The ownerDocument property of newChild and this node are different.


NO_MODIFICATION_ALLOWED_ERR

This node is read-only and does not allow insertions, or the parent of newChild is read-only and does not allow deletions.


NOT_FOUND_ERR

refChild is not a child of this node.

25.214.2. Description

This method inserts the node newChild into the document tree as a child of this node. The new node is positioned within this node's childNodes[] array so that it comes immediately before the refChild node. If refChild is null, newChild is inserted at the end of childNodes[], just as with the appendChild( ) method. Note that it is illegal to call this method with a refChild that is not a child of this node.

If newChild is already in the document tree, it is removed from the tree and then reinserted at its new position. If newChild is a DocumentFragment node, it is not inserted itself; instead, each of its children is inserted, in order, at the specified location.

25.214.3. Example

The following function inserts a new paragraph at the beginning of a document:

 function insertMessage(message) {     var paragraph = document.createElement("p");  // Create a <p> Element     var text = document.createTextNode(message);  // Create a Text node     paragraph.appendChild(text);                  // Add text to the paragraph     // Now insert the paragraph before the first child of the body     document.body.insertBefore(paragraph, document.body.firstChild) } 

25.214.4. See Also

Node.appendChild( ), Node.removeChild( ), Node.replaceChild( )




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net