Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Nuestros voluntarios aún no han traducido este artículo al Español. Únete a nosotros y ayúdanos a traducirlo

The Node.compareDocumentPosition() method compares the position of the current node against another node in any other document.

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

Syntax

node.compareDocumentPosition( otherNode ) 

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.

Note: First statement uses NodeList method item(0), which is equivalent to getElementsByTagName('head')[0].

Specifications

Specification Status Comment
DOM
The definition of 'Node.compareDocumentPosition()' in that specification.
Living Standard  
Document Object Model (DOM) Level 3 Core Specification
The definition of 'Node.compareDocumentPosition()' in that specification.
Recommendation Initial definition

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) 9.0 (Yes) (Yes)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) ? (Yes) (Yes)

See also

Etiquetas y colaboradores del documento

Etiquetas: 
 Última actualización por: vmsynkov,