Introduction to Ranges

I just wrote an Introduction to Ranges. Ideally this would be the start of an article series similar to my Event series. I'm not going to promise anything, though; I'm too thinly spread as it is, and I have no idea when I'll continue working on this series.

I've been working on a Range compatibility table, and even though it's not even remotely finished I'll officially unveil it now; chances are it'll take me quite a while to create proper test pages and test the dozens of methods and properties I haven't (yet) needed in my Range project.

This is the blog of Peter-Paul Koch, web developer, consultant, and trainer. You can also follow him on Twitter or Mastodon.
Atom RSS

If you like this blog, why not donate a little bit of money to help me pay my bills?



Comments are closed.

1 Posted by Nikola Ivanov on 11 May 2007 | Permalink

Great article, PPK, it took me only a few minutes to read it. I am excited about the next chapters.

2 Posted by Julien Royer on 11 May 2007 | Permalink

Very interesting. Thank you!

3 Posted by Milo van der Leij on 11 May 2007 | Permalink

Good article. Although, I would like to see more details on how this applies specifically to text in Input and TextArea elements, including coverage of Mozilla's selectionStart/selectionEnd properties.

4 Posted by Denuziere Christophe on 11 May 2007 | Permalink

Always problem with navigators compatibility :(.
Also very good article.

5 Posted by James Oppenheim on 12 May 2007 | Permalink

Excellent article. Interesting to see yet more inconsistent behavior with IE.

6 Posted by medyk on 12 May 2007 | Permalink

Great article.
There's one error - Microsoft Text Range specification link leads to Mozilla Selection specification.

7 Posted by ppk on 12 May 2007 | Permalink

Thanks Medyk, corrected.

Milo: if you want more tests, do them and put the results online. I'll link to you.

8 Posted by sandro turriate on 12 May 2007 | Permalink

Great article indeed! This is really strange, I just became interested in finding information on text selections via JS last night. I did a few google searches today and was happy to see Quirksmode in the results. I knew you'd outline the problem (and solution) better than most so the last thing to do was see how old the article was. "Page last changed today." Brilliant!

Thanks for all your work, You've helped me tremendously over the past 1.5 years.

9 Posted by Milo van der Leij on 14 May 2007 | Permalink

PPK: Unfortunately I can't do that because [insert random generic excuse here]. I can point out, however, that Microsoft's algorithm for finding the cursor position in a textarea ( ) is laughable, and that there are other "solutions" out there ( ) that people will unfortunately copy and paste.

A slightly modified version of finally "scratched my itch". It works for Input fields, which is all I needed. TextAreas may or may not work because some people ( ) say there's inconsistency in the way IE counts newlines.

10 Posted by Andreas on 24 May 2007 | Permalink

Very interesting. Thank you!

11 Posted by Jacco on 3 June 2007 | Permalink

A rangeCount of zero may not seem likely, but it is possible. So I do think it is necessary to check it before using getRangeAt(0).

If the rangeCount property is 0, getRangeAt(0) will cause an error in Firefox. Opera 9 returns an HTMLDocument object (bit strange).

The line document.selection.createRange() can also return a controlrange object (at least in designMode). So in designMode checking the type property of document.selection is necessary.