首页 文章

如何检测IE11?

提问于
浏览
195

当我想检测IE时我使用此代码:

function getInternetExplorerVersion()
{
  var rv = -1;
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    msg = "You are using IE " + ver;
  }
  alert( msg );
}

但IE11正在返回“你没有使用Internet Explorer” . 我怎样才能发现它?

15 回答

  • 15

    我在带滚动条的元素处使用了 onscroll 事件 . 在IE中触发时,我添加了以下验证:

    onscroll="if (document.activeElement==this) ignoreHideOptions()"
    
  • 2

    我已经阅读了你的答案并做了一个混合 . 它似乎适用于Windows XP(IE7 / IE8)和Windows 7(IE9 / IE10 / IE11) .

    function ie_ver(){  
        var iev=0;
        var ieold = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
        var trident = !!navigator.userAgent.match(/Trident\/7.0/);
        var rv=navigator.userAgent.indexOf("rv:11.0");
    
        if (ieold) iev=new Number(RegExp.$1);
        if (navigator.appVersion.indexOf("MSIE 10") != -1) iev=10;
        if (trident&&rv!=-1) iev=11;
    
        return iev;         
    }
    

    当然,如果我返回0,则表示没有IE .

  • 8

    我正在使用一种更简单的方法:

    导航器全局对象具有属性接触点,在Internet Exlorer 11中称为msMaxTouchPoints .

    所以如果你寻找:

    navigator.msMaxTouchPoints !== void 0
    

    您将找到Internet Explorer 11 .

  • 35

    使用 !(window.ActiveXObject) && "ActiveXObject" in window 显式检测IE11 .

    要检测任何IE(pre-Edge,"Trident")版本,请改用 "ActiveXObject" in window .

  • 0
    var ua = navigator.userAgent.toString().toLowerCase();
    var match = /(trident)(?:.*rv:([\w.]+))?/.exec(ua) ||/(msie) ([\w.]+)/.exec(ua)||['',null,-1];
    var rv = match[2];
    return rv;
    
  • 2

    试试这个:

    var trident = !!navigator.userAgent.match(/Trident\/7.0/);
    var net = !!navigator.userAgent.match(/.NET4.0E/);
    var IE11 = trident && net
    var IEold = ( navigator.userAgent.match(/MSIE/i) ? true : false );
    if(IE11 || IEold){
    alert("IE")
    }else{
    alert("Other")
    }
    
  • 210

    方案:

    function GetIEVersion() {
      var sAgent = window.navigator.userAgent;
      var Idx = sAgent.indexOf("MSIE");
      // If IE, return version number.
      if (Idx > 0)
        return parseInt(sAgent.substring(Idx+ 5, sAgent.indexOf(".", Idx)));
    
      // If IE 11 then look for Updated user agent string.
      else if (!!navigator.userAgent.match(/Trident\/7\./))
        return 11;
    
      else
        return 0; //It is not IE
    
    }
    if ((GetIEVersion() > 0) || (navigator.userAgent.toLowerCase().indexOf('firefox') > -1)){
      alert("This is IE " + GetIEVersion());
    }else {
      alert("This no is IE ");
    }
    
  • 11

    使用 MSInputMethodContext 作为特征检测检查的一部分 . 例如:

    //Appends true for IE11, false otherwise
    window.location.hash = !!window.MSInputMethodContext && !!document.documentMode;
    

    References

  • 81

    这似乎是一种更好的方法 . 如果没有匹配,“indexOf”返回-1 . 它不会覆盖正文上的现有类,只需添加它们即可 .

    // add a class on the body ie IE 10/11
    var uA = navigator.userAgent;
    if(uA.indexOf('Trident') != -1 && uA.indexOf('rv:11') != -1){
        document.body.className = document.body.className+' ie11';
    }
    if(uA.indexOf('Trident') != -1 && uA.indexOf('MSIE 10.0') != -1){
        document.body.className = document.body.className+' ie10';
    }
    
  • 0

    从User-Agent获取IE版本

    var ie = 0;
    try { ie = navigator.userAgent.match( /(MSIE |Trident.*rv[ :])([0-9]+)/ )[ 2 ]; }
    catch(e){}
    

    How it works: 用户代理字符串for all IE versions包括“MSIE空间版本" or "三叉戟其他文本rv空格或冒号版本”部分 . 知道这一点,我们从 String.match() 正则表达式中获取版本号 . try-catch 块用于缩短代码,否则我们需要测试非IE浏览器的数组边界 .

    Note: 如果用户将浏览器设置为"compatibility mode",则可能会欺骗或省略用户代理,有时会无意中 . 虽然这在实践中似乎不是一个问题 .


    在没有User-Agent的情况下获取IE版本

    var d = document, w = window;
    var ie = ( !!w.MSInputMethodContext ? 11 : !d.all ? 99 : w.atob ? 10 : 
    d.addEventListener ? 9 : d.querySelector ? 8 : w.XMLHttpRequest ? 7 : 
    d.compatMode ? 6 : w.attachEvent ? 5 : 1 );
    

    How it works: 每个版本的IE都添加了对以前版本中找不到的_220352的支持 . 因此,我们可以自上而下的方式测试功能 . 这里使用ternary序列是为了简洁起见,尽管 if-thenswitch 语句也可以正常工作 . 变量 ie 设置为整数5-11,对于较旧的设置为1,对于较新/非IE设置为99 . 如果您只想准确测试IE 1-11,可以将其设置为0 .

    Note: 如果您的代码在包含第三方脚本的页面上运行,对象检测可能会中断,这些脚本会为 document.addEventListener 之类的内容添加polyfill . 在这种情况下,用户代理是最佳选择 .


    检测浏览器是否为现代

    如果您只对浏览器是否支持大多数HTML 5和CSS 3标准感兴趣,您可以reasonably assume IE 8及更低版本仍然是主要的问题应用程序 . 测试 window.getComputedStyle 将为您提供相当好的现代浏览器组合(IE 9,FF 4,Chrome 11,Safari 5,Opera 11.5) . IE 9大大提高了标准支持,但原生CSS动画需要IE 10 .

    var isModernBrowser = ( !document.all || ( document.all && document.addEventListener ) );
    
  • 7

    Angular JS就是这样做的 .

    msie = parseInt((/msie (\d+)/.exec(navigator.userAgent.toLowerCase()) || [])[1]);
    if (isNaN(msie)) {
      msie = parseInt((/trident\/.*; rv:(\d+)/.exec(navigator.userAgent.toLowerCase()) || [])[1]);
    }
    

    msie will be positive number if its IE and NaN for other browser like chrome,firefox.

    为什么?

    从Internet Explorer 11开始,用户代理字符串已发生显着变化 .

    参考:

    msdn #1 msdn #2

  • 0

    仅适用于IE浏览器:

    var ie = 'NotIE'; //IE5-11, Edge+
        if( !!document.compatMode ) {
            if( !("ActiveXObject" in window) ) ) ie = 'EDGE';
            if( !!document.uniqueID){
                if('ActiveXObject' in window && !window.createPopup ){ ie = 11; }
                else if(!!document.all){
                        if(!!window.atob){ie = 10;}
                        else if(!!document.addEventListener) {ie = 9;}
                        else if(!!document.querySelector){ie = 8;}
                        else if(!!window.XMLHttpRequest){ie = 7;}
                        else if(!!document.compatMode){ie = 6;}
                        else ie = 5;
                    }
            }
        }
    

    使用警报(即);

    测试:

    var browserVersionExplorer = (function() {
        var ie = '<s>NotIE</s>',
            me = '<s>NotIE</s>';
    
        if (/msie\s|trident\/|edge\//i.test(window.navigator.userAgent) && !!(document.documentMode || document.uniqueID || window.ActiveXObject || window.MSInputMethodContext)) {
                if (!!window.MSInputMethodContext) {
                    ie = !("ActiveXObject" in window) ? 'EDGE' : 11;
                } else if (!!document.uniqueID) {
                    if (!!(window.ActiveXObject && document.all)) {
                        if (document.compatMode == "CSS1Compat" && !!window.DOMParser ) {
                            ie = !!window.XMLHttpRequest ? 7 : 6;
                        } else {
                            ie = !!(window.createPopup && document.getElementById) ? parseFloat('5.5') : 5;
                        }
                        if (!!document.documentMode && !!document.querySelector ) {
                            ie = !!(window.atob && window.matchMedia) ? 10 : ( !!document.addEventListener ? 9 : 8);
                        }
                    } else ie = !!document.all ? 4 : (!!window.navigator ? 3 : 2);
                }
            }
            
        return ie > 1 ? 'IE ' + ie : ie;
    })();
    
     alert(browserVersionExplorer);
    

    Update 01 Jun 2017

    现在我们可以使用更简单,更简单的东西:

    var uA = window.navigator.userAgent,
        onlyIEorEdge = /msie\s|trident\/|edge\//i.test(uA) && !!( document.uniqueID || window.MSInputMethodContext),
        checkVersion = (onlyIEorEdge && +(/(edge\/|rv:|msie\s)([\d.]+)/i.exec(uA)[2])) || NaN;
    
  • 0

    坦率地说,我会说使用一个可以满足你需要的库(比如platform.js) . 在某些时候,事情会发生变化,图书馆将配备这些更改,使用正则表达式进行手动解析将失败 .

    感谢上帝IE走了......

  • 1

    根据this list of changes,IE11不再报告为 MSIE ,故意避免错误检测 .

    如果你真的想知道IE,那么你可以做的就是检测用户代理中的 Trident/ 字符串,如果 navigator.appName 返回 Netscape ,就像(未经测试的);

    function getInternetExplorerVersion()
    {
      var rv = -1;
      if (navigator.appName == 'Microsoft Internet Explorer')
      {
        var ua = navigator.userAgent;
        var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
          rv = parseFloat( RegExp.$1 );
      }
      else if (navigator.appName == 'Netscape')
      {
        var ua = navigator.userAgent;
        var re  = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
          rv = parseFloat( RegExp.$1 );
      }
      return rv;
    }
    
    console.log('IE version:', getInternetExplorerVersion());
    

    请注意,IE11(afaik)仍处于预览状态,用户代理可能会在发布前更改 .

  • 3

    用以下方法检测大多数浏览器:

    var getBrowser = function(){
      var navigatorObj = navigator.appName,
          userAgentObj = navigator.userAgent,
          matchVersion;
      var match = userAgentObj.match(/(opera|chrome|safari|firefox|msie|trident)\/?\s*(\.?\d+(\.\d+)*)/i);
      if( match && (matchVersion = userAgentObj.match(/version\/([\.\d]+)/i)) !== null) match[2] = matchVersion[1];
      //mobile
      if (navigator.userAgent.match(/iPhone|Android|webOS|iPad/i)) {
        return match ? [match[1], match[2], mobile] : [navigatorObj, navigator.appVersion, mobile];
      }
      // web browser
      return match ? [match[1], match[2]] : [navigatorObj, navigator.appVersion, '-?'];
    };
    

    https://gist.github.com/earlonrails/5266945

相关问题