Приглашаем посетить
Чулков (chulkov.lit-info.ru)

Accessing the User's Selection

Accessing the User's Selection

User selection is closely related to the TextRange object. The document's selection property references an object that exposes the current selection in the browser to scripts. This selection object also exposes a type property that returns the type of the selection: None for no selection and Text for a text-based selection. When text is selected on the screen, the selection object's createRange method returns a TextRange object that spans the selected text. Repositioning this TextRange object will not change what text is selected on the screen, but changes made to the text by the TextRange object will of course be reflected on the screen. To change the selection on the screen to match a TextRange object, you can call the TextRange object's select method. For example, the following code uses a TextRange object to extend the selection on the screen by one word:

if ("Text" == document.selection.type) {
   var tr = document.selection.createRange();
   // Move the end position to include one more word.
   tr.moveEnd("word", 1);
   // Reselect the range.
   tr.select();
}
else 
   alert("No text is selected.");

Always test the type of the selection before you do any manipulating because the browser supports a second type of object, called a ControlRange, for selecting multiple controls. Because multiple controls are currently not selectable when you are browsing a document, the ControlRange object is not discussed here.

[Содержание]