GetBoundingClientRect problem with Firefox

I am trying to get the y coordinate of the cursor inside a contenteditable div using getBoundingClientRect (). Unfolding IE code works, but another branch (i.e. Firefox 3.5 for my current testing purposes) does not.

In the code below there are problematic lines marked with a *** in the comment. At this point in the code, both selObj and selRange have a value (confirmed in Firebug), but I cannot call getBoundingClientRect () for any of them (for example, selObj.getBoundingClientRect is not a function). I read that getBoundingClientRect () is now supported by Firefox in the Range object, but I cannot get it to work. I think I should call it the wrong type of object ...? What should I call?

The following code is a complete test case in the form of an html file containing the appropriate javascript. Viewed in IE, I get the value for the y coordinate of the cursor, but it appears in Firefox.

<html> <head> <title>Test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style> #pageContainer { padding:50px; } .pageCommon { width: 100px; height: 100px; background-color:#ffffD0; padding: 10px; border: 1px solid black; overflow: auto; } </style> </head> <body> <div id="pageContainer"> <div id="editor" class="pageCommon" contenteditable onclick="setPageNav();" onkeypress="setPageNav();"> </div> <div>y: <span id="y"></span></div> </div> <script> var y; function setPageNav() { page = document.getElementById("editor"); if (window.getSelection) { var selObj = window.getSelection(); var selRange = selObj.getRangeAt(0); // *** Neither of these next two lines work, error is : selObj.getBoundingClientRect is not a function y = selObj.getBoundingClientRect().top; y = selRange.getBoundingClientRect().top; } else if (document.selection) { var range = document.selection.createRange(); y = range.getBoundingClientRect().top; } document.getElementById("y").innerHTML = y; } </script> </body> </html> 
+4
source share
2 answers

I read that getBoundingClientRect () is now supported by Firefox in a Range object

This is not so far. This is the Mozilla 1.9.3 feature that you can expect in Firefox 3.7.

In any case, you will need a reserve, since it is not supported by any other browsers.

+4
source

I read that getBoundingClientRect () is now supported by Firefox in a Range object

Support for this new in Gecko 1.9.3 alpha , which will be included in the version of Firefox after 3.6.x. Firefox 3.5 does not support it.

+2
source

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


All Articles