Does chrome supports document.selection?
Solution 1
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.
Solution 2
Try document.getSelection()
or window.getSelection()
.
Here's a quick example that I tested in chrome
Solution 3
Use window.getSelection()
instead.
https://developer.mozilla.org/en/DOM/window.getSelection
Solution 4
Browser support for the selection
object based on IE11 and Chrome 87.04280.141
Member | IE | Chrome |
---|---|---|
document.selection |
yes | no |
window.selection |
no | no |
document.getSelection() |
no | yes |
window.getSelection() |
no | yes |
This is the easy part.
The problems come when you are trying to use any methods, f.e. getRange()
which exists for document.selection
(IE compatible), but doesn't exist for document.getSelection()
, so for Chrome you need a workaround.
adi
Updated on June 23, 2022Comments
-
adi almost 2 years
I am new to javascript, trying to perform:
document.selection.createRange();
but
document.selection
always returnsundefined
.I am using latest version of chrome.
what am I doing wrong?
thanks!