I use this simple Javascript code to retrieve text that was selected on a webpage:
var userselection = window.getSelection(); //user selection will be a Selection-Object
var rangeObject = userselection.getRangeAt(0);
The code works fine if text is selected. If I just click in the text that could be selected I get a range object too, it is collapsed as expected.
But if I don't select nor click, Firefox throws this error:
uncaught exception: [Exception... "Component returned failure code: 0x80070057
(NS_ERROR_ILLEGAL_VALUE) [nsISelection.getRangeAt]" nsresult: "0x80070057
(NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: file:///home/jan/Webprojekte/test-range
selection.html# :: who :: line 16" data: no]
Is there a clean way to prevent the error but still notificate the user that no text was selected?