admin管理员组

文章数量:1207717

I'm executing these simple rows of javascript in latest ie 11, just to select all content of a div

Here a screenshot from ie11 dev tool

Like you can see, IE alter me saying: "unable to complete the operation due to error 800a025e".

I'm not able to understand the nature, the source, of the problem, and no others stack overflow questions I read give me a clear answer.

This is full code of my selectText jQuery "personal" extension

jQuery.fn.selectText = function(){
    var doc = document;
    var element = this[0];
    // console.log(this, element);
    if (typeof element == 'undefined') {
        return;
    }
    if (doc.body.createTextRange) {
        var range = document.body.createTextRange();
        range.moveToElementText(element);
        range.select();
    } else if (window.getSelection) {
        var selection = window.getSelection();        
        var range = document.createRange();
        range.selectNodeContents(element);
        selection.removeAllRanges();
        selection.addRange(range);
    }
};

Element in this case is a

[Object HTMLTableElement]

Edit 1: with a little mod:

var range = document.body.createTextRange();
var retval = range.moveToElementText(element);
console.log (retval);
range.select();

I'm able to say you retval is undefined. This code work on ff without problems, so element selector is fine. jQuery version is 1.11

I'm executing these simple rows of javascript in latest ie 11, just to select all content of a div

Here a screenshot from ie11 dev tool

Like you can see, IE alter me saying: "unable to complete the operation due to error 800a025e".

I'm not able to understand the nature, the source, of the problem, and no others stack overflow questions I read give me a clear answer.

This is full code of my selectText jQuery "personal" extension

jQuery.fn.selectText = function(){
    var doc = document;
    var element = this[0];
    // console.log(this, element);
    if (typeof element == 'undefined') {
        return;
    }
    if (doc.body.createTextRange) {
        var range = document.body.createTextRange();
        range.moveToElementText(element);
        range.select();
    } else if (window.getSelection) {
        var selection = window.getSelection();        
        var range = document.createRange();
        range.selectNodeContents(element);
        selection.removeAllRanges();
        selection.addRange(range);
    }
};

Element in this case is a

[Object HTMLTableElement]

Edit 1: with a little mod:

var range = document.body.createTextRange();
var retval = range.moveToElementText(element);
console.log (retval);
range.select();

I'm able to say you retval is undefined. This code work on ff without problems, so element selector is fine. jQuery version is 1.11

Share Improve this question edited Apr 7, 2014 at 13:45 realtebo asked Apr 7, 2014 at 13:34 realteborealtebo 25.6k44 gold badges125 silver badges215 bronze badges 6
  • var retval = range .moveToElementText(element); What is retval? – epascarello Commented Apr 7, 2014 at 13:39
  • Yes, but I'm programming in php / js, on a linux server ... – realtebo Commented Apr 7, 2014 at 13:39
  • You said this problem occurs in IE 11, which is not supported on Linux as far as I'm aware. – Raymond Chen Commented Apr 7, 2014 at 13:41
  • ?!?!?! I'm programming php page served by a linux server. I'm watching page from ie 11 from a windows pc... – realtebo Commented Apr 7, 2014 at 13:44
  • @epascarello: retval is undefined – realtebo Commented Apr 7, 2014 at 13:44
 |  Show 1 more comment

3 Answers 3

Reset to default 9

I resolved using this trick:

I wrapped the html table with a DIV

I use the same code, as is, with no changes, against the DIV, instead of using for selecting only the table.

It's working.

Probably html table is not a selectable text for IE

None of the above suggestions or hints properly resolves this particular problem.

The issue itself is caused by non-text (for example table) elements getting collected together with other elements within a TextRange collection. Attempting to clear the collection while these table elements remain within the collection will unconditionally cause the reported error above.

The issue affects IE 9/10/11, and there are specific workarounds for each of those browsers. Unfortunately those workarounds are not compatible with other browsers (firefox, safari, chrome, edge), so we need to special case the clear handling for IE 9/10/11 while providing alternate handling for more standards compliant browsers. The following appears to work everywhere, at least as of this writing:

      if (doc.body.createTextRange) { // All IE but Edge
        var range = doc.body.createTextRange();
        range.collapse();
        range.select();
      }
      else {
        doc.getSelection().removeAllRanges();
      }

The IE specific branch is a roundabout way of clearing a range containing table elements without actually invoking the empty() method which otherwise results in the troublesome exception.

I got this error when trying to window.getSelection().removeAllRanges(); and there was no selection. One workaround is to check if there is a selection first:

if (window.getSelection().getRangeAt(0).getClientRects.length > 0) {
    window.getSelection().removeAllRanges();
}

Also, this question is a duplicate of Could not complete the operation due to error 800a025e, but I couldn't mark it as such because the other question doesn't have an accepted answers.

本文标签: internet explorer 11Javascript error 800a025e using range selectorStack Overflow