R
RobG
A general method for getting the selected text in a document (other
than inside input and textarea elements) is:
function getSelectedText() {
// supported by most browsers
// Concatenate an empty string to fix bug in
// Safari <2.0 and Firefox < 1.0.3
if (window.getSelection) {
return window.getSelection() + '';
// Supported by browsers that support the above,
// and IE 9 (per MSDN, untested)
} else if (document.getSelection) {
return document.getSelection();
// For IE < 9 and similar
} else if (document.selection &&
document.selection.createRange) {
return document.selection.createRange().text;
// If all else fails...
} else {
return null;
}
}
In researching improvements to the above, I started looking for
documentation on the various methods used. However, I'm having
difficulty tracking down documentation of the interfaces that
implement the DOM getSelection method.
MDC describes window.getSelection as DOM 0[1], it doesn't have a page
for document.getSelection although it's listed as a method of the
document object[2].
MSDN has an entry for document.getSelection[3], however it references
a non-existent part of the HTML5 document. Apparently getSelection has
been made part of a separate DOM Range document[4], whose status seems
to be "work in progress".
It seems to me that getSelection should be part of the document
interface, however I have looked in the DOM 2 HTML specification and
the HTML5 document and I can't find getSelection list as a method of
any API - Node, HTMLElement, HTMLDocument or HTML5 Window.
Can someone tell me where I can find it? Alternatively, explain
whether document.getSelection should be preferred over
window.getSelection for some other reason than the former "works" in
recent IE and the later doesn't.
MDC window.getSelection
1. <URL: https://developer.mozilla.org/en/DOM/window.getSelection#Specification
MDC document methods
2. <URL: https://developer.mozilla.org/en/DOM/document#Methods >
MSDN document.getSelection
3. <URL: http://msdn.microsoft.com/en-us/library/ff975169(v=VS.85).aspx
DOM Range
4. <URL: http://html5.org/specs/dom-range.html >
than inside input and textarea elements) is:
function getSelectedText() {
// supported by most browsers
// Concatenate an empty string to fix bug in
// Safari <2.0 and Firefox < 1.0.3
if (window.getSelection) {
return window.getSelection() + '';
// Supported by browsers that support the above,
// and IE 9 (per MSDN, untested)
} else if (document.getSelection) {
return document.getSelection();
// For IE < 9 and similar
} else if (document.selection &&
document.selection.createRange) {
return document.selection.createRange().text;
// If all else fails...
} else {
return null;
}
}
In researching improvements to the above, I started looking for
documentation on the various methods used. However, I'm having
difficulty tracking down documentation of the interfaces that
implement the DOM getSelection method.
MDC describes window.getSelection as DOM 0[1], it doesn't have a page
for document.getSelection although it's listed as a method of the
document object[2].
MSDN has an entry for document.getSelection[3], however it references
a non-existent part of the HTML5 document. Apparently getSelection has
been made part of a separate DOM Range document[4], whose status seems
to be "work in progress".
It seems to me that getSelection should be part of the document
interface, however I have looked in the DOM 2 HTML specification and
the HTML5 document and I can't find getSelection list as a method of
any API - Node, HTMLElement, HTMLDocument or HTML5 Window.
Can someone tell me where I can find it? Alternatively, explain
whether document.getSelection should be preferred over
window.getSelection for some other reason than the former "works" in
recent IE and the later doesn't.
MDC window.getSelection
1. <URL: https://developer.mozilla.org/en/DOM/window.getSelection#Specification
MDC document methods
2. <URL: https://developer.mozilla.org/en/DOM/document#Methods >
MSDN document.getSelection
3. <URL: http://msdn.microsoft.com/en-us/library/ff975169(v=VS.85).aspx
DOM Range
4. <URL: http://html5.org/specs/dom-range.html >