1

I was told about textRange in IE, but I can't find equivalent in other browsers for it. I need a way to read the element's X,Y offsets that the viewer is seeing.

Just a student
  • 10,560
  • 2
  • 41
  • 69
Himberjack
  • 5,682
  • 18
  • 71
  • 115

1 Answers1

2

Firefox has Range object instead of TextRange in IE - https://developer.mozilla.org/en/DOM/range

From the first look I didn't see the methods that return coordinates, but they have something that will come in 4.0 that might help:

https://developer.mozilla.org/en/DOM/range.getBoundingClientRect

Returns a ClientRect object that bounds the contents of the range; this a rectangle enclosing the union of the bounding rectangles for all the elements in the range.

You can also look here: https://developer.mozilla.org/en/DOM%3aElement.getClientRects

The returned value is a collection of ClientRect objects, one for each CSS border box associated with the element. Each ClientRect object contains read-only left, top, right and bottom properties describing the border box, in pixels, with the top-left relative to the top-left of the viewport, unless the element is inside an SVG foreignobject element, in which case the top-left is relative to the nearest foreignobject ancestor and in the coordinate system of that foreignobject. For tables with captions, the caption is included even though it's outside the border box of the table.

Originally, Microsoft intended this method to return a TextRectangle object for each line of text. However, the CSSOM working draft specifies that it returns a ClientRect for each border box. For an inline element, the two definitions are the same. But for a block element, Mozilla will return only a single rectangle.

To select:

IE:

var range = document.body.createTextRange();
range.moveToElementText(myDiv);
range.select();

Firefox, Opera, WebKit nightlies:

var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(myDiv);
selection.removeAllRanges();
selection.addRange(range);

Safari:

var selection = window.getSelection();
selection.setBaseAndExtent(myDiv, 0, myDiv, 1);
Roman Goyenko
  • 6,965
  • 5
  • 48
  • 81
  • I need FF 3+, is there a way to select inner text? – Himberjack Nov 10 '10 at 16:45
  • It seems that there is no difference between the properties of the letter 'o' & 'F' when selecting each one of them and reading their offsets. is there an explanation of this? I am trying to get coordinates of each letter... (the TOP PIXEL of every letter) – Himberjack Nov 10 '10 at 17:05
  • It probably tells the coordinates of the square that the selection covers and it looks to be the same for all the letters, no matter capital or not. I am not sure you could get the glyph coordinates in the browser, you might be better off using SVG for that if it is possible. – Roman Goyenko Nov 10 '10 at 17:13
  • 1
    That last version for Safari is only necessary for really old versions. The second version will work since around Safari 3.0 (not sure exactly). – Tim Down Nov 10 '10 at 17:18