• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Using Ranges

To create a new range in the Netscape Navigator, you can use the document object’s createRange method. Then you can use various methods to position the range, such as setStart, setEnd, selectNode, selectNodeContents, and so on. And you can use various properties and methods you see in Table 11.20 and 11.21 to work with the range. (Unfortunately, one of the most attractive methods, insertNode, which lets you insert or replace text in a range, isn’t available in the Netscape Navigator yet.)

Here’s an example. In this case, I’ll display some text in a web page, some of which will be bold, and I’ll use createRange to create a range corresponding to the bold text. Next, I’ll let the user select some text, and when the user does, create a new range and use the Range object’s compareBoundaryPoints method to let the user know whether her selection is before the bold text, after it, or overlaps it. How do we get a Range object corresponding to the current selected text? There’s not much known about the Netscape Navigator selection object yet, but it does have a getRangeAt method, and we can get a Range object starting at the beginning of the selection by calling selection.getRangeAt(0). Here’s what the code looks like (NS6+ only). Note that it creates the first range, range1, which corresponds to the bold text, as soon as the page loads:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint