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!
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!
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.
Try document.getSelection()
or window.getSelection()
.
Here's a quick example that I tested in chrome
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.