XML DOM compareDocumentPosition()Methods


Element object

Definition and Usage

The compareDocumentPosition() method compares the document position of the current node with the specified node according to document order.

Syntax

elementNode.compareDocumentPostition(node)

Parameter Description
node Required. Specifies the node to compare with the current node.


Example

The following code snippet usesloadXMLDoc()put "books.xml" Load into xmlDoc, and compare the first and third <book> nodes:

Example

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book')[0];
y=xmlDoc.getElementsByTagName('book')[2];

document.write(x.compareDocumentPosition(y));

Output:

4

Try it Yourself »

Firefox and most other browsers treat empty spaces or line breaks generated between nodes as text nodes, while Internet Explorer ignores white-space text nodes generated between nodes. Therefore, in the example above, Mozilla browsers will output 4, while Internet Explorer will output 2.

To learn more about browser differences, please visit our section in the XML DOM tutorial.DOM browserChapter.


Element objectother extensions