-3

When selecting all the children of an element I sometimes receive the following error in Gecko/Waterfox:

Error: NS_ERROR_FAILURE

This implies (in a very unhelpful fashion) that the element is (for some reason or another) not selectable. Here is an example of such code:

var e = document.getElementById('example');

if (e.childNodes.length > 0)
{
 window.getSelection().removeAllRanges();
 window.getSelection().selectAllChildren(e);
}

How do I determine if an element is selectable? Obviously, as the JavaScript implies, there are child elements in my tests implying that the element should be selectable. Absolutely no frameworks/libraries/etc.

John
  • 1
  • 13
  • 98
  • 177

1 Answers1

0

There is cheap, affordable and expensive. The vast majority of times one should strive for an affordable answer. Unfortunately it appears that cheap is appropriate in this circumstance. Using 1 millisecond delay with setTimeout seems to have eliminated the issue.

var e = document.getElementById('example');

if (e.childNodes.length > 0)
{
 window.getSelection().removeAllRanges();
 setTimeout(function() {window.getSelection().selectAllChildren(e);},1);
}

This is clearly a Gecko bug on the internal timing mechanisms however after filing dozens of quality bug reports at Mozilla for over a decade that have fallen on deaf ears anyone encountering this issue will just have to waste a few more bytes until I take over Mozilla.

Similar Bugs...

The error message is very generic so to determine if the error has to do with the internal timing mechanisms of the JavaScript engine (that developers working with JavaScript should never have to be concerned with) it is critical to ensure that every mouse movement, click, etc is reproduced and in the same order. If it appears that the exact same actions sometimes fail without any predictability other than simply happening in non-deterministic fashion then using setTimeout might help alleviate such issues though ultimately it is a browser bug that should be fixed.

John
  • 1
  • 13
  • 98
  • 177