Search code examples
javascriptinternet-explorerinternet-explorer-11

Internet Explorer 11 detection


I know IE 11 has different user agent string than all other IE

 Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko

I have tried to detect IE 11 with answer specified for this question'

Jquery fail to detect IE 11

Thats !!navigator.userAgent.match(/Trident\/7\./)

But I am getting error Object not found and needs to be re-evaluated.

Then I openede developer console in IE11 and tried to access some predefined javascript objects, I am still getting same error.

I have tried

navigator.userAgent

window.navigator

console.log('test');

Anyone have any idea about it ?


Solution

  • Edit 18 Nov 2016

    This code also work (for those who prefer another solution , without using ActiveX)

    var isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
      // true on IE11
      // false on Edge and other IEs/browsers.
    

    Original Answer

    In order to check Ie11 , you can use this : ( tested)

    (or run this)

    !(window.ActiveXObject) && "ActiveXObject" in window

    I have all VMS of IE :

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    Notice : this wont work for IE11 :

    as you can see here , it returns true :

    enter image description here

    So what can we do :

    Apparently , they added the machine bit space :

    ie11 :

    "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko"
    

    ie12 :

    "Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko"
    

    so we can do:

    /x64|x32/ig.test(window.navigator.userAgent)
    

    this will return true only for ie11.