DOM:window.getSelection
From MDC
Contents |
[edit] Summary
Returns a selection object representing the range of text selected by the user.
[edit] Syntax
selection = window.getSelection() ;
-
selectionis a Selection object.
[edit] Example
function foo() {
var selObj = window.getSelection();
alert(selObj);
var selRange = selObj.getRangeAt(0);
// do stuff with the range
}
[edit] Notes
In JavaScript, when a selection object is passed to a function expecting a string (like window.alert or document.write), a string representation of it (i.e. the selected text) is passed instead. This makes the selection object appear like a string, when it is really an object with its own properties and methods. Specifically, the return value of calling the toString() method of the Selection object is passed.
In the above example, selObj is automatically "converted" when passed to window.alert. However, to use a JavaScript String property or method such as length or substr, you must manually call the toString method.
[edit] Specification
DOM Level 0. Not part of specification.