mozilla

Node.compareDocumentPosition Redirect 1

API« DOM Reference

Summary

Compares the position of the current node against another node in any other document.

Syntax

node.compareDocumentPosition( otherNode ) 
  • node is the node that's being compared.
  • otherNode is the node that's being compared against.

The return value is computed as the relationship that otherNode has with node.

Notes

The return value is a bitmask with the following values:

Name Value
DOCUMENT_POSITION_DISCONNECTED 1
DOCUMENT_POSITION_PRECEDING 2
DOCUMENT_POSITION_FOLLOWING 4
DOCUMENT_POSITION_CONTAINS 8
DOCUMENT_POSITION_CONTAINED_BY 16
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC 32

Example

var head = document.getElementsByTagName('head').item(0);
if (head.compareDocumentPosition(document.body) & Node.DOCUMENT_POSITION_FOLLOWING) {
  console.log("well-formed document");
} else {
  console.log("<head> is not before <body>");
}

Note: Because the result returned by compareDocumentPosition is a bitmask, the bitwise and operator has to be used for meaningful results.

Specifications

Specification Status Comment
DOM Level 3 Recommendation  
DOM Standard Living standard  

See also

Document Tags and Contributors

Contributors to this page: Sheppy
Last updated by: Sheppy,