In slight contrast to many languages which are referred to as "object-oriented" (a concept we'll briefly explore later in this chapter), JavaScript is actually an "object-based" language. In JavaScript, practically everything is an object, except for language constructs, keywords, and operators. Objects in
JavaScript play many different roles, from representing data types, to manipulation of (X)HTML documents via the Document Object Model (DOM),
to interfacing with the browser, and more.
Object-based programming
in JavaScript
Conclusion
JavaScript provides four types of objects: user-defined, native, host, and document. This chapter focused on the fundamental aspects of all objects, as well as the creation and use of user-defined objects. JavaScript is a prototype-based, object-oriented language.
Output
getSelection() and Selection Object
This page contains examples for the Selection Object defined in http://html5.org/specs/dom-range.html#selection as well as the IE<9 alternatives
Returns a selection object representing the selected content in the current document.
In IE < 9, use selection object.
the getSelection method does not return the selection if the selected content is within an input:password, input:text or textarea element. For those cases, use the selectionStart and selectionEnd properties.
The selection object represents the current selection and every Range object that belongs to the selection object represents a contiguous part of the selection.
In Internet Explorer, Opera, Google Chrome, Safari and Firefox before version 3, at most one Range can belong to the selectionRange object, because text selection is always a contiguous part of the DOM hierarchy.
In Firefox from version 3, multiple areas of text can be selected by holding down the CTRL key while selecting.
- anchorNode - Returns the element that contains the start of the selection.
- anchorOffset - Returns the offset of the start of the selection relative to the element that contains the start of the selection.
- focusNode - Returns the element that contains the end of the selection.
- focusOffset - Returns the offset of the end of the selection relative to the element that contains the end of the selection.
- isCollapsed - Returns true if there's no selection or if the selection is empty. Otherwise, returns false.
- rangeCount - Returns the number of ranges in the selection. A Range is each selection. Use ctrl to make more than one selection.
- void collapse(in Node parentNode, in long offset) - Replaces the selection with an empty one at the given position. If this is in a text area, the cursor will be placed there.
- void collapseToStart() - Replaces the selection with an empty one at the position of the start of the current selection.
- void collapseToEnd(); - Replaces the selection with an empty one at the position of the end of the current selection.
- void selectAllChildren(in Node parentNode); -Replaces the selection with one that contains all the contents of the given element.
- void deleteFromDocument(); - Deletes the selection.
- Range getRangeAt(in long index); - Returns the given range.
- void addRange(in Range range); - Adds the given range to the selection.
- void removeRange(in Range range); - Removes the given range from the selection, if the range was one of the ones in the selection.
- void removeAllRanges(); - Removes all the ranges in the selection.
extend - moves the focus point of the current selectionRange object to the specified point.