Window.getSelection (), how do you determine if the node anchor is in front of the node focus?

I only want to allow the selection from left to right, so the node anchor will always be the first node in the DOM tree (relative to the focus of the node).

Is there an easy way to check if the node anchor is in front of the node focus?

+4
source share
1 answer

Here's an easy way to do this, which exploits the fact that setting the end of the DOM range at an earlier point in the document than the beginning of the range will change the range. I think this will break in Firefox 2, which had an error in processing it, but the number of users of this browser is tiny.

function isSelectionBackwards() { var backwards = false; if (window.getSelection) { var sel = window.getSelection(); if (!sel.isCollapsed) { var range = document.createRange(); range.setStart(sel.anchorNode, sel.anchorOffset); range.setEnd(sel.focusNode, sel.focusOffset); backwards = range.collapsed; range.detach(); } } return backwards; } 
+7
source

Source: https://habr.com/ru/post/1380017/


All Articles