25

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"

Kevin Heidt
  • 1,135
  • 1
  • 11
  • 14
  • 2
    Pretty sure this was one of the deliberate changes being made to IE11 to have it avoid being caught in legacy IE checks. – BoltClock Oct 28 '13 at 15:39

7 Answers7

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

Joe
  • 41,484
  • 20
  • 104
  • 125
  • 9
    Thanks Joe, that link is helpful. I found a jquery ticket related to window.ActiveXObject at [link](http://bugs.jquery.com/ticket/14475) which points to [link](http://msdn.microsoft.com/en-us/library/ff955298(v=vs.85).aspx) that explains ActiveXOBject detection will fail within a conditional (my OP example). The jquery ticket suggestion of using `window.ActiveXObject !== undefined` worked, as did another suggestion from elsewhere of using `"ActiveXObject" in window`. I saw the try/catch method used as well, but I don't want to instantiate an activex, only check if it exists and is therefore IE – Kevin Heidt Oct 30 '13 at 16:43
  • 1
    Trying to detect IE in this way is shortsighted, as it's explicitly what the IE team is trying to prevent, and their compatibility lie is likely to improve in a future version. Instead, Feature Detect the actual features you need. – EricLaw Feb 10 '14 at 16:31
  • 2
    @EricLaw, what if the feature you need is ActiveX? I don't want to instantiate an actual ActiveX object just to check that. – Tom Winter Nov 06 '14 at 21:32
  • @TomWinter: Your question is: "My website requires ActiveX. But I don't want to use ActiveX." I don't think I understand that question. – EricLaw Nov 06 '14 at 23:32
  • 2
    @EricLaw, If I'm running under IE I need to use MSXML, an ActiveX object, because the native XML stuff in IE doesn't support something I need (document.evaluate - as far as I can tell; if I'm wrong I'd love to know ;-) Outside of IE I need to use native, obviously. I have some global functions that do XML stuff and I need to set them to the right implementation (MSXML vs Native) when the page loads. I don't want to instantiate an MSXML doc just to do that. I supposed I could lazy check the first time an XML function is called but that's a pain. I want an easy way. ;-) – Tom Winter Nov 07 '14 at 17:59
20

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);
mhu
  • 17,720
  • 10
  • 62
  • 93
  • Yes it works, as per the above answer. ActiveXObject is undefined in IE11. So it may exist in `window` but as `undefined`. So don't do that. Do `this.supportActiveX = (window.ActiveXObject !== undefined);` – hexalys Nov 27 '13 at 22:29
  • Found a better check using getOwnPropertyDescriptor: https://groups.google.com/d/msg/comp.lang.javascript/EOziMLM_zEc/kudaP8ng50IJ – mhu Apr 23 '14 at 19:57
6

You can use following code to detect IE

var iedetect = 0;
if(window.ActiveXObject || "ActiveXObject" in window)
{
    iedetect = 1;
}
Chetan S
  • 935
  • 1
  • 11
  • 21
4

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
Community
  • 1
  • 1
Garlaro
  • 149
  • 3
  • 7
3

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.

tlfu
  • 171
  • 4
2

I hate to be "that guy", but

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

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

Jacob
  • 29
  • 5
1

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;
}
Aldekein
  • 3,538
  • 2
  • 29
  • 33