Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #4209 > unrolled thread

getSelection - method of document or window?

Started byRobG <rgqld@iinet.net.au>
First post2011-07-14 21:22 -0700
Last post2011-07-16 18:53 +0100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  getSelection - method of document or window? RobG <rgqld@iinet.net.au> - 2011-07-14 21:22 -0700
    Re: getSelection - method of document or window? RobG <rgqld@iinet.net.au> - 2011-07-14 22:41 -0700
    Re: getSelection - method of document or window? Dr J R Stockton <reply1128@merlyn.demon.co.uk> - 2011-07-16 18:53 +0100

#4209 — getSelection - method of document or window?

FromRobG <rgqld@iinet.net.au>
Date2011-07-14 21:22 -0700
SubjectgetSelection - method of document or window?
Message-ID<b108c664-9f71-4519-98c9-216429f42a73@m3g2000pre.googlegroups.com>
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%28v=VS.85%29.aspx
>

DOM Range
4. <URL: http://html5.org/specs/dom-range.html >


--
Rob

[toc] | [next] | [standalone]


#4211

FromRobG <rgqld@iinet.net.au>
Date2011-07-14 22:41 -0700
Message-ID<d93d93f2-638e-44c0-ae06-d20e0c8e1f48@t38g2000prj.googlegroups.com>
In reply to#4209
On Jul 15, 2:22 pm, RobG <rg...@iinet.net.au> wrote:
[...]
> 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.

--
Rob

[toc] | [prev] | [next] | [standalone]


#4235

FromDr J R Stockton <reply1128@merlyn.demon.co.uk>
Date2011-07-16 18:53 +0100
Message-ID<1aBEJaCVAdIOFwVX@invalid.uk.co.demon.merlyn.invalid>
In reply to#4209
In comp.lang.javascript message <b108c664-9f71-4519-98c9-216429f42a73@m3
g2000pre.googlegroups.com>, Thu, 14 Jul 2011 21:22:21, RobG
<rgqld@iinet.net.au> posted:

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

-- 
 (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk  Turnpike 6.05  WinXP.
 Web  <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
 Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
 DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web