javascripthtmlinternet-explorerdom-selection

Character offset in an Internet Explorer TextRange


As far as I can tell there's no simple way of retrieving a character offset from a TextRange object in Internet Explorer. The W3C Range object has a node, and the offset into the text within that node. IE seems to just have pixel offsets. There are methods to create, extend and compare ranges, so it would be possible to write an algorithm to calculate the character offset, but I feel I must be missing something.

So, what's the easiest way to calculate the character offset of the start of an Internet Explorer TextRange?


Solution

  • I'd suggest IERange, or just the TextRange-to-DOM Range algorithm from it.

    Update, 9 August 2011

    I'd now suggest using my own Rangy library, which is similar in idea to IERange but much more fully realized and supported.