Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML DOM compareDocumentPosition() Method

XML DOM compareDocumentPosition() Method


Node Object Reference Node object


Definition and Usage

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

Syntax

nodeObject.compareDocumentPostition(node)

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

Example

The following code snippet uses loadXMLDoc() to load "books.xml" into xmlDoc, and compare the document positions of the first and third elements:

Example

xmlDoc=loadXMLDoc("books.xml");

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

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

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

Output:

4

Try it »

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

To learn more about browser differences, please visit our DOM Browsers section.


Node Object Reference Node object