Javascript Reference
Categories: selection

javascript selection Description

@Aug. 17, 2009, 8:43 a.m.
selection Firefox/Netscape/NN 6 IE 4(Win) Chrome/Safari/DOM n/a  

 

  

The selection object represents zero or more characters that have been explicitly selected in a document by the user or selected under script control. The objects are very different entities in the IE and Navigator browsers (observe compatibility ratings for properties and methods, below), and each has its own ways of providing script access to it.

 

In IE for Windows, you create a selection object via the document.selection property, which returns a selection object. To perform substantive actions on the content of the selection object, you then generate a TextRange object from the selection object (via the selection object's createRange( ) method). Use TextRange properties and methods to interact with the content. To convert a TextRange object to a visibly selected stretch of text on the page, use the TextRange object's select( ) method. This close linkage with the TextRange object means that the IE selection object is so far limited to Win32 versions. The IE selection object can include selected text inside an input (of type text) and textarea element.

 

In IE for the Macintosh, you don't have a selection object per se. Instead, it implements the Navigator 4 document.getSelection( ) method, which returns only the string contents of the selected text.

 

Navigator 4 offers script access to the text selected in a document through the use of the document.getSelection( ) method. This method is deprecated in Netscape 6, and even displays a warning (less harmful than an error) in the JavaScript Console if you use the method. In its place, Netscape 6 implements a robust selection object that offers a long list of properties and methods to interact with the object. Most of this functionality was made available starting with Netscape 6.2, including the way to create a selection object: the window.getSelection( ) method. Notice that many properties and methods of the Netscape 6 selection object have analogs with the Range object specification. In fact, it is through the Range object that scripts can highlight even discontiguous text spans on the page: create and size a Range object; then add that Range to the highlighted text via the selection object's addRange( ) method. Netscape 6 selections (as with the Range object) operate only on body content, and not on text inside editable text boxes.

 

Be aware that clicking on buttons in earlier browsers (including IE 5 for the Mac) deselects the current text selection. Therefore, all scripted action involving selections in these browsers must be triggered by onselect or onmouseup events, or functions invoked by a timer (see the window.setTimeout( ) method). More recent browsers maintain content selections while buttons are pressed.

 
Object Model Reference
 
 
Object-Specific Properties
 
anchorNode anchorOffset focusNode focusOffset
isCollapsed rangeCount type typeDetail
 
Object-Specific Methods
 
addRange( ) clear( ) collapse( )
collapseToEnd( ) collapseToStart( ) containsNode( )
createRange( ) createRangeCollection( ) deleteFromDocument( )
empty( ) extend( ) getRangeAt( )
removeAllRanges( ) removeRange( ) selectAllChildren( )
selectionLanguageChange( ) toString( )  
 
Object-Specific Event Handler Properties

None.


Powered by Linode.