window.ActiveXObject difference in IE11

天大地大妈咪最大 提交于 2019-11-28 17:32:20

You can't use that check for IE11:

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.)

The following works in IE11:

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

But this better and more reliable:

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

You can use following code to detect IE

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

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

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.

Garlaro

Can't add comment to this answer, sorry.

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

ActiveXObject detection will fail when performed within a conditional statement

In IE11

"ActiveXObject" in window
> true

and

typeof window.ActiveXObject
> "undefined"

but (this is where IE lies)

window.ActiveXObject !== undefined
> true

so apparently only this check is reliable

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

IE10

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

I hate to be "that guy", but

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

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!