The selection Object


The selection Object

Both the Internet Explorer and the Netscape Navigator support a selection object, and, of course, they're different. In fact, no public documentation is available for the Netscape Navigator's selection object yet. You can see the properties of the Internet Explorer's selection object in Table 11.16, its methods in Table 11.17, and its events in Table 11.18. You can access this object for a document with the document object's selection property (see Table 9.1).

Tip

Although Netscape has yet to document their selection object, we do know that it contains a getRangeAt method, and I put that method to work in "Using Ranges" later in this chapter.


Table 11.16. The Properties of the selection Object

Property

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

type

           

x

x

x

x

 

Read-only

 

Retrieves the type of selection, as a string. Possible values: "none" (there is no selection), "text" (the selection is a text selection), or "control" (the selection is a control).

typeDetail

               

x

x

 

Read-only

 

Some nonbrowser applications can provide a custom selections and can set values for this property that indicate the selection type. The implementation of this property in the Internet Explorer returns undefined.

TextRange

               

x

x

 

Read-only

 

Holds an array of the TextRange objects in the selection. An individual text range can be accessed as selection . TextRange( index [ , subIndex ]) . Here, index is an integer or string that indicates the element or collection to get. If this parameter is an integer, this method returns the text range at the given position. If this parameter is a string and there is more than one text range with the name or id property equal to that string, this method returns all matching elements as an array. The subIndex parameter is used when index is a string and more than one text range has the same name or id property, and lets you specify which text range to retrieve.

Table 11.17. The Methods of the selection Object

Method

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

clear

           

x

x

x

x

 

Returns: Nothing

 

Clears the contents of the selection.

 

Syntax: selection .clear() .

createRange

           

x

x

x

x

 

Returns: TextRange object

 

Creates a TextRange object from the current text selection. Sytnax: selection .createRange() .

CreateRangeCollection

               

x

x

 

Returns: Collection of TextRange objects

 

Creates a TextRange object collection from the current selectioncurrently, the whole selection is returned in one TextRange object. Theoretically, nonbrowser applications can support multiple selections and can return a collection of TextRange objects.

empty

           

x

x

x

x

 

Returns: Nothing

 

Cancels the current selection, sets the selection type to none, and sets the item property to null . Sytnax: selection . empty() .

Table 11.18. The Events of the selection Object

Property

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

ontimeerror

See Chapter 6.

 

In fact, we've already seen how to use the Internet Explorer's selection object in Chapter 9 (see Listing 09-07.html on the web site). In that example, you could make a selection using the mouse, and as soon as you did, the selected text appeared in a text field.

Here's the code:

 <HTML>      <HEAD>          <TITLE>Reading Selected Text</TITLE>          <SCRIPT LANGUAGE="JavaScript">              <!--  function getSelected()   {   if (navigator.appName == "Microsoft Internet Explorer") {   document.form1.text1.value = document.selection.createRange().text   }   if(navigator.appName == "Netscape") {   document.form1.text1.value = document.getSelection()   }   }  // -->          </SCRIPT>      </HEAD>      <BODY ONMOUSEUP="getSelected()">          <H1>Reading Selected Text</H1>          Select some of this text!          <BR>          <FORM NAME="form1">              You selected: <INPUT TYPE="TEXT" NAME="text1">          </FORM>      </BODY>  </HTML> 

You can see the results in Chapter 9, Figure 9.4, where I'm selecting some text and the code is displaying it.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net