XML DOM compareDocumentPosition()Methods
Node object
Definition and Usage
The compareDocumentPosition() method compares the document position of the current node with the specified node according to document order.
Syntax
nodeObject.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 document positions of the first and third <book> elements:
Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book')[0];
y=xmlDoc.getElementsByTagName('book')[2];
document.write(x.compareDocumentPosition(y));
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 whitespace 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 in our XML DOM tutorialDOM browserChapter.
Node objectother extensions