当我想检测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”。我如何检测它?


当前回答

仅适用于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;
                }
        }
    }

使用alert (ie);

测试:

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

2017年6月1日更新

现在我们可以用一些更简单的东西:

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;

其他回答

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

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

试试这个:

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")
}

从User-Agent获取IE版本

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

工作原理:所有IE版本的用户代理字符串都包含一部分“MSIE空间版本”或“Trident其他文本rv空格或冒号版本”。知道了这一点,我们从String.match()正则表达式中获取版本号。try-catch块用于缩短代码,否则我们需要测试非ie浏览器的数组边界。

注意:如果用户将浏览器设置为“兼容模式”,用户代理可能会被欺骗或省略,有时是无意的。尽管这在实践中似乎不是什么大问题。


获取没有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 );

工作原理:每个版本的IE都增加了对以前版本中没有的额外功能的支持。因此,我们可以以自顶向下的方式测试特性。为了简洁起见,这里使用了三元序列,不过if-then和switch语句也同样有效。变量ie被设置为整数5-11,旧的为1,新/非ie为99。如果你只是想测试IE 1-11,你可以把它设置为0。

注意:如果您的代码运行在带有第三方脚本的页面上,这些脚本为document.addEventListener之类的东西添加了腻子,那么对象检测可能会中断。在这种情况下,用户代理是最好的选择。


检测浏览器是否是现代的

如果你只对浏览器是否支持大多数HTML 5和css3标准感兴趣,你可以合理地假设ie8及更低版本的应用程序仍然是主要的问题应用程序。测试窗口。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 ) ); 

坦率地说,我会说使用你需要的库(比如platform.js)。在某些时候,事情会发生变化,库将为这些变化做好准备,使用正则表达式的手动解析将失败。

感谢上帝IE消失了…

在window中使用!(window.ActiveXObject) && "ActiveXObject"显式检测IE11。

要检测任何IE (pre-Edge,“Trident”)版本,在窗口中使用“ActiveXObject”代替。