This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.
The comparePoint() method of the Range interface determines whether a specified point is before, within, or after the Range. The point is specified by a reference node and an offset within that node.
comparePoint(referenceNode, offset)
referenceNodeThe Node that the offset is relative to.
offsetAn integer greater than or equal to zero describing the position inside referenceNode of the point to be checked. If referenceNode is a Node of type Text, Comment, or CDATASection, then offset is the number of characters from the start of referenceNode. For other Node types, offset is the number of child nodes from the start of the referenceNode.
A number.
-1 if the point specified by the referenceNode and offset is before the start of this Range.0 if the point specified by the referenceNode and offset is within this Range (including the start and end points of the range).1 if the point specified by the referenceNode and offset is after the end of this Range.const text = new Text("0123456789");
const thisRange = new Range();
thisRange.setStart(text, 1);
thisRange.setEnd(text, 6);
thisRange.comparePoint(text, 3); // 0
thisRange.comparePoint(text, 0); // -1
thisRange.comparePoint(text, 6); // 0
thisRange.comparePoint(text, 7); // 1
| Specification |
|---|
| DOM> # ref-for-dom-range-comparepoint①> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
comparePoint |
1 | 17 | 1 | ≤12.1 | 3 | 18 | 4 | ≤12.1 | 1 | 1.0 | 4.4 | 1 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Range/comparePoint