Compares the position of two nodes in a document.
Syntaxvar positionBitmask = node.compareDocumentPosition(/* see parameter list */);Parameters otherNode
The node to be compared to the reference node, which is the node executing the method.
Returns an object of type NumberNumber
Describes how the position of the node specified by the otherNode parameter relates to the position of the reference node. The return value contains one or more of the following flag values:
Node constant name Return value (Integer) Return value (Hexadecimal) Description DOCUMENT_POSITION_DISCONNECTED 1 0x01 The nodes are disconnected. DOCUMENT_POSITION_PRECEDING 2 0x02 The position of the node specified by the otherNode parameter precedes the position of the reference node. DOCUMENT_POSITION_FOLLOWING 4 0x04 The position of the node specified by the otherNode parameter follows the position of the reference node. DOCUMENT_POSITION_CONTAINS 8 0x08 The reference node contains the node specified by the otherNode parameter. DOCUMENT_POSITION_CONTAINED_BY 16 0x10 The node specified by the otherNode parameter contains the reference node. DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC 32 0x20 The node positions depend on the DOM implementation and cannot be compared. ExamplesThis example determines if the web document is incorrectly marked up with the body element before the head element. Note: Web browsers sometimes self correct markup errors. HTML5 is the first specification to give guidelines to browser vendors on how they should correct markup errors.
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>"); }Notes
For information about the factors that affect the position of nodes within the DOM, see the related specifications.
Related specificationsMozilla Developer Network : [Node.compareDocumentPosition Article]
Microsoft Developer Network: [compareDocumentPosition Method Article]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4