The compareDocumentPosition() method compares the placement of two nodes in the DOM hierarchy (document).
The compareDocumentPosition() method is supported in all major browsers.
Note: Internet Explorer 9 and earlier do not support this method.
Parameter | Type | Description |
---|---|---|
node | Node object | Required. Specifies the node to compare with the current node |
Type | Description |
---|---|
Number | A Number representing where the two nodes are positioned compared to each other. The possible return values
are:
1 - No relationship, the two nodes do not belong to the same document. 2 - The specified node precedes the current node. 4 - The specified node follows the current node. 8 - The specified node contains the current node. 16 - The specified node is contained by the current node. 32 - The specified and the current node have no common container node or the two nodes are different attributes of the same node. Note: The return value could also be a combination of values. E.g. a return value of 20 means that the specified node is contained by the current node (16) AND the specified node follows the current node (4). |
DOM Version | Core Level 3 Node Object |
---|
The following code fragment loads "books.xml" into xmlDoc using loadXMLDoc() and compares the placement of two nodes (the first and the third <book> element) in the DOM hierarchy:
Output: