Microsoft HomeproductssearchsupportshopWrite Us   Microsoft Home
Magazine
 |  Community
 |  Workshop
 |  Tools & Samples
 |  Training
 |  Site Info

Workshop  |  XML (Extensible Markup Language)

insertBefore Method


Inserts a child node to the left of the specified node or at the end of the list.

Syntax

objDOMNode  = oDOMNode.insertBefore(newChild, refChild)

Parameters

newChild
Object. Address of the new node to be inserted.
refChild
Variant. Address of the reference node; newChild is inserted to the left of refChild. If NULL, newChild is inserted at the end of the child list.

Returns

Object. On success, returns the child node that was inserted.

Remarks

This operation depends on the value of the nodeType property:

NODE_ATTRIBUTE

This operation depends on the value of the newChild parameter:

NODE_ATTRIBUTE
,
NODE_CDATA_SECTION
,
NODE_COMMENT
,
NODE_DOCUMENT
,
NODE_DOCUMENT_TYPE
,
NODE_ELEMENT
,
NODE_ENTITY
,
NODE_NOTATION
,
NODE_PROCESSING_INSTRUCTION
Returns an error. These node types cannot be children of an attribute.
NODE_DOCUMENT_FRAGMENT
Inserts the children of the document fragment (newChild) and returns newChild.
NODE_ENTITY_REFERENCE
,
NODE_TEXT
Inserts newChild and returns newChild.
NODE_CDATA_SECTION
,
NODE_COMMENT
,
NODE_ENTITY
,
NODE_NOTATION
,
NODE_PROCESSING_INSTRUCTION
,
NODE_TEXT
Returns an error. These node types either cannot have children or their children are read-only.
NODE_DOCUMENT

This operation depends on the value of the newChild parameter:

NODE_ATTRIBUTE
,
NODE_CDATA_SECTION
,
NODE_DOCUMENT
,
NODE_ENTITY
,
NODE_ENTITY_REFERENCE
,
NODE_NOTATION
,
NODE_TEXT
Returns an error. These nodes are not valid as children of a document node.
NODE_COMMENT
,
NODE_PROCESSING_INSTRUCTION
Inserts newChild and returns newChild.
NODE_DOCUMENT_TYPE
,
NODE_ELEMENT
Inserts newChild and returns newChild. Note, however, that by definition an XML document (the Document node) can have only a single child. Therefore, an error is returned if the document node already has a child.
NODE_DOCUMENT_FRAGMENT
Inserts the children of the document fragment (in newChild) and returns newChild. Note that the insert operations are subject to the rules for child nodes and can fail if the document fragment children represent node types that cannot be inserted.
NODE_DOCUMENT_TYPE
Returns an error. The document type is read-only.
NODE_DOCUMENT_FRAGMENT

This operation depends on the value of the newChild parameter:

NODE_ATTRIBUTE
,
NODE_DOCUMENT
,
NODE_DOCUMENT_TYPE
Returns an error. These nodes types are not valid as children of a document fragment.
NODE_CDATA_SECTION
,
NODE_COMMENT
,
NODE_ELEMENT
,
NODE_ENTITY_REFERENCE
,
NODE_PROCESSING_INSTRUCTION
,
NODE_TEXT
Inserts newChild and returns newChild.
NODE_DOCUMENT_FRAGMENT
Inserts the children of the document fragment (newChild) and returns newChild.
NODE_ENTITY
,
NODE_NOTATION
Returns an error. Entities and notations are read-only and cannot be inserted into a document.
NODE_ELEMENT

This operation depends on the value of the newChild parameter:

NODE_CDATA_SECTION
,
NODE_COMMENT
,
NODE_ELEMENT
,
NODE_ENTITY_REFERENCE
,
NODE_TEXT
,
NODE_PROCESSING_INSTRUCTION
Inserts newChild and returns newChild.
NODE_ATTRIBUTE
,
NODE_DOCUMENT
,
NODE_DOCUMENT_TYPE
,
NODE_ENTITY
,
NODE_NOTATION
Returns an error. These node types cannot be children of an element node.
NODE_DOCUMENT_FRAGMENT
Inserts the children of the document fragment (newChild) and returns newChild.
NODE_ENTITY_REFERENCE
Returns an error. Although the child nodes of an entity reference are the expanded entity, the children cannot be modified.

The node supplied in refChild must be a child node of this node or null. The newChild is inserted before refChild (as the left sibling) in the child list. If refChild is null, newChild is inserted at the end of the child list. If refChild is not a child of this node, an error is returned.

If newChild is a node of type DOCUMENT_FRAGMENT, all its children are inserted, in the same order, before refChild. If newChild is already in the tree, it is first removed.

The parameters can represent nodes in the same document or in different documents. When within the same document, the nodes retain their default attributes and data types. When the parameters represent nodes in different documents, the nodes either lose or alter their default attributes, depending on whether this node's document has a DTD (or other schema). The Microsoft implementation attempts to correctly merge the different DTDs (document type definitions).

If the newChild node's DTD or schema differs from the original document, the nodes will be treated with the definitions in the new DTD, including default attributes and data types. If there is no DTD, the nodes keep their data types by picking up an instance definition (attributes lose their data type), and default attributes are lost. Note also that cutting and pasting between documents with two different DTDs can result in an invalid document that can fail to parse after being saved.

When elements are added as children to other elements, they introduce their own namespace scoping. That is, if the element is from a default namespace, the default has scope beginning with the element being added.

If an entity reference is being added as a child to an element or document fragment, the namespace declaration of the entity reference is added to the containing element. Similarly, if the entity reference is being added to an attribute, the namespace and the prefix of the entity reference are added to the element containing the attribute.

If there is a conflict between prefixes on the containing element and the prefixes being added, the insertBefore operation fails and returns an error. For example, a conflict occurs when a new attribute referring to a different namespace is added to an element with the namespace, as in the following:

xmlns:myns="URN1"

The error could result from the new attribute, where "myns" refers to a different namespace, "URN2", such as would result from a call to createNode ("attribute","myns:myname","URN2"):

myns:myname="myattributevalue"

When adding a document fragment, the containing element adds all namespaces and prefixes from the document fragment. If this causes a conflict on the containing element, insertBefore returns an error.

Applies To


Does this content meet your programming needs? Write us!

Back to topBack to top

© 1998 Microsoft Corporation. All rights reserved. Terms of use.

 

Magazine Home
Ask Jane
DHTML Dude
Extreme XML
For Starters
More or Hess
Servin' It Up
Site Lights
Web Men Talking
Member Community Home
Benefits: Freebies & Discounts
Benefits: Promote Your Site
Benefits: Connect with Your Peers
Benefits at a Glance
Online Special-Interest Groups
Your Membership
SBN Stores
Join Now
Workshop Home
Essentials
Content & Component Delivery
Component Development
Data Access & Databases
Design
DHTML, HTML & CSS
Extensible Markup Language (XML)
Languages & Development Tools
Messaging & Collaboration
Networking, Protocols & Data Formats
Reusing Browser Technology
Security & Cryptography
Server Technologies
Streaming & Interactive Media
Web Content Management
Workshop Index
Tools & Samples Home
Tools
Samples, Headers, Libs
Images
Sounds
Style Sheets
Web Fonts
Training Home
SBN Live Seminars
SBN Live Chats
Courses
Peer Support
CD-ROM Training
Books & Training Kits
Certification
SBN Home
New to SBN?
What's New on SBN
Site Map
Site Search
Glossary
Write Us
About This Site