javascript window.ActiveXObject 与 IE11 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19638981/
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-10-27 16:17:36  来源:igfitidea点击:

window.ActiveXObject difference in IE11

javascriptinternet-explorer-11

提问by Kevin Heidt

While looking over our site in IE11 to find out what's broken now, we noticed that the below code doesn't evaluate to "true" correctly:

在 IE11 中查看我们的网站以找出现在有什么问题时,我们注意到以下代码没有正确评估为“true”:

this.isIEBrowser = false;
if (window.ActiveXObject){
    this.isIEBrowser = true;
}

Upon further investigation, it appears that typeof(window.ActiveXObject)results in "undefined", whereas in IE10 mode, it results in "function". When I add window.ActiveXObjectto the watch list, it shows as being a function type. Similarly, if I do typeof(window.ActiveXObject.prototype), I get "object"for both IE11 and IE10.

经过进一步调查,似乎typeof(window.ActiveXObject)导致"undefined",而在 IE10 模式下,它导致"function"。当我将window.ActiveXObject添加到监视列表时,它显示为函数类型。同样,如果我执行typeof(window.ActiveXObject.prototype),我会得到IE11 和 IE10 的“对象”

Does anybody know why this changed, or where I can find a list of these types of differences between IE10 and IE11 so that I can figure out what other breaking changes there are?

有谁知道为什么会发生这种变化,或者我可以在哪里找到 IE10 和 IE11 之间这些类型差异的列表,以便我可以找出还有哪些其他重大变化?



UPDATE 10/30/13:

2013 年 10 月 30 日更新:

When I put this in, I had originally thought this was a difference with Type evaluation in the IE11 javascript engine. I've since come to realize that this issue is specific to the window.ActiveXObject object. So I've changed the name of this question from "Typeof difference in IE11" to "window.ActiveXObject difference in IE11"

当我把它放进去的时候,我原本以为这与 IE11 javascript 引擎中的类型评估不同。我已经意识到这个问题是特定于 window.ActiveXObject 对象的。所以我将这个问题的名称从“IE11 中的 Typeof 差异”更改为“IE11 中的 window.ActiveXObject 差异”

采纳答案by Joe

You can't use that check for IE11:

您不能对 IE11 使用该检查:

http://msdn.microsoft.com/en-us/library/ie/dn423948%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/ie/dn423948%28v=vs.85%29.aspx

Starting with IE11, the navigator object supports plugins and mimeTypes properties. In addition, the window.ActiveXObject property is hidden from the DOM. (This means you can no longer use the property to detect IE11.)

从 IE11 开始,导航器对象支持插件和 mimeTypes 属性。此外,window.ActiveXObject 属性对 DOM 是隐藏的。(这意味着您不能再使用该属性来检测 IE11。)

回答by mhu

The following works in IE11:

以下在 IE11 中有效:

this.supportActiveX = ("ActiveXObject" in window);

But this better and more reliable:

但这更好,更可靠:

this.supportActiveX = 
    (Object.getOwnPropertyDescriptor && Object.getOwnPropertyDescriptor(window, "ActiveXObject")) || 
    ("ActiveXObject" in window);

回答by Chetan S

You can use following code to detect IE

您可以使用以下代码来检测 IE

var iedetect = 0;
if(window.ActiveXObject || "ActiveXObject" in window)
{
    iedetect = 1;
}

回答by Garlaro

Can't add comment to this answer, sorry.

无法对此答案添加评论,抱歉。

I found that in IE11 you can't use"ActiveXObject" in windowin order to check for the actual ActiveX support.

我发现在 IE11 中你不能使用"ActiveXObject" in window来检查实际的 ActiveX 支持。

ActiveXObject detection will fail when performed within a conditional statement

在条件语句中执行时,ActiveXObject 检测将失败

In IE11

在 IE11

"ActiveXObject" in window
> true

and

typeof window.ActiveXObject
> "undefined"

but (this is where IE lies)

但是(这是 IE 所在的地方)

window.ActiveXObject !== undefined
> true

so apparently only this check is reliable

所以显然只有这个检查是可靠的

typeof window.ActiveXObject !== "undefined"
> false

IE10

IE10

typeof window.ActiveXObject !== "undefined"
> true

回答by tlfu

Actually, what I'm observing is that in IE9 both of these evaluate to true:

实际上,我观察到的是,在 IE9 中,这两个结果都为真:

this.supportActiveX = (typeof window.ActiveXObject !== 'undefined');

this.supportActiveX = ("ActiveXObject" in window);

But in IE11, the first statement evaluates to false, while the second is true. I don't have an explanation for this behavior, but it suggests that the ("ActiveXObject" in window) option is more reliable.

但是在 IE11 中,第一个语句的计算结果为 false,而第二个语句为 true。我没有对这种行为的解释,但它表明(窗口中的“ActiveXObject”)选项更可靠。

回答by Jacob

I hate to be "that guy", but

我讨厌成为“那个人”,但是

 this.supportActiveX = (typeof window.ActiveXObject !== 'undefined')

is slightly safer than mhu's answer since undefined is assignable.

比 mhu 的答案更安全,因为 undefined 是可分配的。

回答by Aldekein

Code sample from our library:

我们库中的代码示例:

if (document.implementation && document.implementation.createDocument && typeof XSLTProcessor != 'undefined') { 
    // chrome, firefox etc
}
else try {
    // IE
    var xml = new ActiveXObject("MSXML2.DOMDocument");
    var xsl = new ActiveXObject("Microsoft.XMLDOM");
}
catch (e) {
    // no support
    console.log('transformxml: no browser support');
    return null;
}