Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does chrome supports document.selection?

I am new to javascript, trying to perform:

document.selection.createRange();

but document.selection always returns undefined.

I am using latest version of chrome.

what am I doing wrong?

thanks!

like image 985
adi Avatar asked Aug 09 '11 22:08

adi


People also ask

What does document getSelection do?

getSelection() The getSelection() property of the Document interface returns a Selection object representing the range of text selected by the user, or the current position of the caret.

What is get Selection?

getSelection() The Window. getSelection() method returns a Selection object representing the range of text selected by the user or the current position of the caret.

What are selections in Javascript?

The basic concept of selection is Range, that is essentially a pair of “boundary points”: range start and range end. A Range object is created without parameters: let range = new Range(); Then we can set the selection boundaries using range.


2 Answers

Use window.getSelection(), which is the most cross-browser compatible (it's supported in the current versions of all major browsers) and is the standard. Chrome certainly supports it as fully as other browsers.

document.selection should only be used for IE < 9.

like image 128
Tim Down Avatar answered Oct 18 '22 22:10

Tim Down


Try document.getSelection() or window.getSelection().

Here's a quick example that I tested in chrome

http://jsfiddle.net/hgDwx/

like image 35
Garett Avatar answered Oct 18 '22 23:10

Garett