Javascript ExtJs4 + IE9 = 对象不支持属性或方法“createContextualFragment”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5375616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:55:26  来源:igfitidea点击:

ExtJs4 + IE9 = Object doesn't support property or method 'createContextualFragment'

javascriptextjsinternet-explorer-9

提问by obenjiro

I'm working with ExtJs on IE9.. and i almost always get this error..

我在 IE9 上使用 ExtJs .. 我几乎总是遇到这个错误..

Microsoft JScript runtime error:

Object doesn't support property or method 'createContextualFragment'

Microsoft JScript 运行时错误:

对象不支持属性或方法“createContextualFragment”

What dose it means? What 'createContextualFragment' is needed for? And how to fix this?

它的意思是什么剂量?需要什么“createContextualFragment”?以及如何解决这个问题?

回答by Tim Down

createContextualFragment()is a method of Rangeobjects that creates a document fragment from an HTML string. It's present in Firefox and WebKit and Opera but is currently non-standard (it's not in the DOM Level 2 Range specbut is in the work-in-progress DOM Parsing and Serialization spec) and IE 9 didn't implement it, which is consistent with Microsoft's general approach to implementing standard functionality in IE 9 that was previously missing in IE. ExtJs must be using this method, although rather foolishly since it is non-standard and the same result can easily be achieved using innerHTML, which is supported everywhere.

createContextualFragment()是一种Range从 HTML 字符串创建文档片段的对象方法。它存在于 Firefox、WebKit 和 Opera 中,但目前是非标准的(它不在DOM Level 2 Range 规范中,而是在进行中的DOM Parsing and Serialization 规范中)并且 IE 9 没有实现它,这是与微软在 IE 9 中实现标准功能的一般方法一致,而以前在 IE 中没有。ExtJs 必须使用这种方法,虽然相当愚蠢,因为它是非标准的,并且使用 可以轻松实现相同的结果innerHTML,它在任何地方都受支持。

UPDATE

更新

You can patch the following into IE 9 since it allows extension of host object prototypes, which previous versions did not. The following is a naive implementation of createContextualFragment()adapted from my Rangy librarybut is suitable for most uses. See this Rangy issuefor details and for a more thorough implementation.

您可以将以下内容修补到 IE 9 中,因为它允许扩展主机对象原型,而以前的版本则不允许。以下是createContextualFragment()改编自我的Rangy 库但适用于大多数用途的简单实现。有关详细信息和更彻底的实现,请参阅此 Rangy 问题

Note that this will not work in IE < 9 because those browsers have no DOM Range implementation.

请注意,这在 IE < 9 中不起作用,因为这些浏览器没有 DOM Range 实现。

if (typeof Range.prototype.createContextualFragment == "undefined") {
    Range.prototype.createContextualFragment = function(html) {
        var startNode = this.startContainer;
        var doc = startNode.nodeType == 9 ? startNode : startNode.ownerDocument;
        var container = doc.createElement("div");
        container.innerHTML = html;
        var frag = doc.createDocumentFragment(), n;
        while ( (n = container.firstChild) ) {
            frag.appendChild(n);
        }
        return frag;
    };
}

回答by csuwldcat

Faster, tighter, no looping, works in IE9+ and all non-shit browsers:

更快、更紧密、无循环,适用于 IE9+ 和所有非垃圾浏览器:

var createContextualFragment = (function(){
  var doc = document.implementation.createHTMLDocument(''),
      range = doc.createRange(),
      body = doc.body;
  return function(html){
    body.innerHTML = html;
    range.selectNodeContents(body);
    return range.extractContents();
  }
})();

回答by aldosa

extjs 3.4.0 fixes this issue. No need to change the code bade. Good work on the library, though.

extjs 3.4.0 修复了这个问题。无需更改代码。不过,图书馆的工作做得很好。

回答by Allie

Well, with a small change, the code posted by Tim Down worked for me :

好吧,稍作改动,Tim Down 发布的代码对我有用:

if (typeof Range.prototype.createContextualFragment == "undefined") {
    Range.prototype.createContextualFragment = function (html) {
        var doc = window.document;
        var container = doc.createElement("div");
        container.innerHTML = html;
        var frag = doc.createDocumentFragment(), n;
        while ((n = container.firstChild)) {
            frag.appendChild(n);
        }
        return frag;
    };
}

回答by shw2

I've just applied Tim Down's fix to our ext 3.3.1 installation, because IE9 still doesn't display our pages correctly without it. In other words, I don't think this fix got into EXTJS 3.3, at least not the public version.

我刚刚对我们的 ext 3.3.1 安装应用了 Tim Down 的修复程序,因为没有它,IE9 仍然无法正确显示我们的页面。换句话说,我不认为这个修复进入 EXTJS 3.3,至少不是公共版本。

回答by Brian Moeskau

This has already been fixed in Ext JS 3.3 IIRC, and I imagine it will be fixed in 4.0 prior to the final release (it hasn't even hit beta as of this writing).

这已经在 Ext JS 3.3 IIRC 中修复了,我想它会在最终版本之前在 4.0 中修复(在撰写本文时它甚至还没有达到测试版)。