window.ActiveXObject difference in IE11

▼魔方 西西 提交于 2019-12-17 23:29:29

问题


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:

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.ActiveXObject to 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.

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?


UPDATE 10/30/13:

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"


回答1:


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




回答2:


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



回答3:


You can use following code to detect IE

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



回答4:


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.




回答5:


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



回答6:


I hate to be "that guy", but

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

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




回答7:


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;
}


来源:https://stackoverflow.com/questions/19638981/window-activexobject-difference-in-ie11

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