14

Also can you please answer this question? how do I get co-ordinates of selected text in an html using javascript document.getSelecttion()

thanks

Community
  • 1
  • 1
priyank
  • 4,634
  • 11
  • 45
  • 52
  • Does this answer your question? [how do I get co-ordinates of selected text in an html using javascript document.getSelecttion()](https://stackoverflow.com/questions/2605133/how-do-i-get-co-ordinates-of-selected-text-in-an-html-using-javascript-document) – alias51 Mar 15 '20 at 11:56
  • You mean `document.selection` perhaps – vsync Sep 20 '20 at 11:32

2 Answers2

10

You get error message Deprecated method document.getSelection() called. Please use window.getSelection() instead." in Firefox which means document.getSelection() is a deprecated method.

vsync
  • 118,978
  • 58
  • 307
  • 400
Ravi Vanapalli
  • 9,805
  • 3
  • 33
  • 43
  • 3
    Not anymore. It's perfectly valid in current Firefox. – zenw0lf Aug 13 '21 at 21:05
  • IMHO `document.getSelection()` is more intuitive than `window.getSelection()`. For example, the `selectionchange` event won't work on `window` object but it works on `document` object, so it makes more sense to stick with `document.getSelection()` to be more consistent. – thdoan Jun 30 '23 at 23:32
1

They seem to be the same:

https://w3c.github.io/selection-api/#dom-document-getselection

getSelection() method
The method must return the selection associated with this if this has an associated browsing context, and it must return null otherwise.

https://w3c.github.io/selection-api/#dom-window-getselection

getSelection() method
The method must invoke and return the result of getSelection() on this's Window.document attribute.

Editors of MDN say they're equivalent:

https://developer.mozilla.org/en-US/docs/Web/API/Document/getSelection

You can call Window.getSelection(), which works identically to Document.getSelection().

https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection

You can call Document.getSelection(), which works identically to Window.getSelection().

biziclop
  • 14,466
  • 3
  • 49
  • 65