getSelection - method of document or window?


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 >
 
Ad

Advertisements

R

RobG

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.
[...]

Damn, I think I just answered my own question;

<URL: http://html5.org/specs/dom-range.html#extensions-to-other-interfaces
The text has spaces, so searching for "document.getSelection" doesn't
work, it must be "document . getSelection".

Cheers.
 
Ad

Advertisements

D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
g2000pre.googlegroups.com>, Thu, 14 Jul 2011 21:22:21, RobG
A general method for getting the selected text in a document (other
than inside input and textarea elements) is:

If you should later wish to extend to using textarea, recall that there
is at least one version of at least one browser in which it was
necessary for "readonly" to be not set at the exact moment of executing
the test grab. Web <http://www.merlyn.demon.co.uk/js-props.htm> E&OE.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top