Internet explorer 如何检测IE11?

Internet explorer 如何检测IE11?,internet-explorer,debugging,internet-explorer-11,browser-detection,forward-compatibility,Internet Explorer,Debugging,Internet Explorer 11,Browser Detection,Forward Compatibility,当我想要检测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) !=

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

IE11不再报告为MSIE,因为它有意避免错误检测

如果你真的想知道它是IE,你可以做的是在navigator.appName返回Netscape时检测用户代理中的Trident/字符串,类似于未测试的

函数getInternetExplorerVersion { var-rv=-1; 如果navigator.appName==“Microsoft Internet Explorer” { var ua=navigator.userAgent; var re=new RegExpMSIE[0-9]{1,}[\\.0-9]{0,}; 如果re.execua!=null rv=parseFloat RegExp.$1; } 如果navigator.appName=='Netscape' { var ua=navigator.userAgent; var re=new RegExpTrident/*rv:[0-9]{1,}[\\.0-9]{0,}; 如果re.execua!=null rv=parseFloat RegExp.$1; } 返回rv; } console.log'IE version:',getInternetExplorerVersion 试试这个:

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")
}
使用!window.ActiveXObject&&ActiveXObject在窗口中显式检测IE11


若要检测任何IE pre-Edge、Trident版本,请在窗口中使用ActiveXObject。

我已经阅读了您的答案并进行了混合。它似乎适用于WindowsXPIE7/IE8和Windows7IE9/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,则表示否。

使用MSInputMethodContext作为功能检测检查的一部分。例如:

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


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

导航器全局对象有一个属性touchpoints,在Internet Exlorer 11中称为MSMAXStouchpoints tho

因此,如果您希望:

navigator.msMaxTouchPoints !== void 0 

您将找到Internet Explorer 11。

这似乎是一种更好的方法。如果没有匹配项,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';
}

角JS就是这样做的

msie = parseInt((/msie (\d+)/.exec(navigator.userAgent.toLowerCase()) || [])[1]);
if (isNaN(msie)) {
  msie = parseInt((/trident\/.*; rv:(\d+)/.exec(navigator.userAgent.toLowerCase()) || [])[1]);
}
如果msie和NaN用于其他浏览器,如chrome和firefox,msie将是正数

为什么?

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

请参阅:


检测大多数浏览器是否具有以下功能:

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, '-?'];
};
我在滚动条的元素中使用了onscroll事件。在IE中触发时,我添加了以下验证:

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

注意:如果用户已将其浏览器设置为兼容模式,则用户代理可能被欺骗或忽略,有时是无意的。虽然这在实践中似乎不是什么大问题

获取没有用户代理的IE版本 工作原理:IE的每个版本都添加了对以前版本中找不到的功能的支持。因此,我们可以自上而下地测试这些特性。为了简洁起见,这里使用了一个序列,尽管if-then和switch语句也可以工作。变量ie设置为整数5-11,或1表示旧版本,或99表示新版本/非ie。如果您只想精确测试ie 1-11,可以将其设置为0

注意:如果您的代码运行在带有第三方脚本的页面上,该脚本为document.addEventListener等内容添加polyfills,则对象检测可能会中断。在这种情况下,用户代理是最好的选择

检测浏览器是否现代化 如果你只对浏览器是否支持大多数HTML5和CSS3标准感兴趣,你可以认为IE8和更低版本仍然是应用程序的主要问题。测试window.getComputedStyle将为您提供相当好的现代浏览器组合,以及IE 9、FF 4、Chrome 11、Safari 5、Opera 11.5。IE9大大改进了对标准的支持,但原生CSS动画需要IE10

var isModernBrowser = ( !document.all || ( document.all && document.addEventListener ) ); 
解决方案:

函数GetIEVersion{ var sAgent=window.navigator.userAgent; var Idx=sAgent.indexOfMSIE; //如果是IE,返回版本号。 如果Idx>0 返回parseIntsAgent.substringIdx+5,sAgent.indexOf.,Idx; //如果是IE 11,则查找更新的用户代理字符串。 否则如果!!navigator.userAgent.match/Trident\/7\/ 返回11; 其他的 返回0;//它不是IE } 如果GetIEVersion>0 | | navigator.userAgent.toLowerCase.indexOf'firefox'>-1{ 这是IE+GetIEVersion; }否则{ 这不是IE; }仅适用于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=函数{ 变量ie=‘NotIE’, 我=‘诺蒂’; if/msie\s | trident\/| edge\//i.testwindow.navigator.userAgent&&!!document.documentMode | | document.uniqueID | window.ActiveXObject | | window.MSInputMethodContext{ if!!window.MSInputMethodContext{ ie=!窗口中的ActiveXObject?'EDGE':11; }else if!!document.uniqueID{ if!!window.ActiveXObject&&document.all{ 如果document.compatMode==CSS1Compat&!!window.DOMParser{ ie=!!window.XMLHttpRequest?7 : 6; }否则{ ie=!!window.createPopup&&document.getElementById?parseFloat'5.5':5; } 如果document.documentMode&!!document.querySelector{ ie=!!window.atob&&window.matchMedia?10:!!document.addEventListener?9:8; } }否则ie=!!什么文件?4 : !!窗口导航器?3 : 2; } } 返回ie>1?“IE'+IE:IE; };
警报浏览器 坦白地说,我会说使用一个库来做你需要的事情,比如platform.js。在某个时刻,事情会发生变化,库将配备这些变化,使用正则表达式的手动解析将失败



谢天谢地,IE消失了…

这是为了避免错误检测——不幸的是,现在IE11发布了,我们的代码只在IE11中被破坏,而正确检测IE是可行的……如果版本不那么重要,我将此解决方案转换为布尔值函数isIE{return navigator.appName==“Microsoft Internet Explorer”| | navigator.appName==“Netscape”和&new RegExpTrident/*rv:[0-9]{1,}[\.0-9]{0,}.execnavigator.userAgent!=null;}@lzkata-根据,IE实际上是遵循标准的。因此,是的,这是有意的,但根据新标准,它是反对旧的html api的。他们这样做的原因是故意的。他们想像这样破坏浏览器检测脚本。从…实际上应该是:“他们想让十亿个网站像这样破坏。”:var isIE11=!!navigator.userAgent.match/Trident\/7\./;请参阅基于用户代理的任何内容都有缺陷。这太容易欺骗了,现在,这可能不是问题,但在我看来,浏览器检测脚本应该有公平的机会检测伪装。我使用条件注释的组合,尝试to强制执行document.documentMode,然后按照下面的Paul Sweatte查看window.MSInputMethodContext。我会发布我的代码,但这是徒劳的。IE11有用户代理:Mozilla/5.0 Windows NT 6.1;WOW64;Trident/7.0;rv:11.0类似Gecko操作系统类型:6.1-win7,6.3-Win81查看我的答案这里是我找到的最佳解决方案:if Object.hasOwnProperty.callwindow、ActiveXObject&!window.ActiveXObject{//is IE11}错误的代码,因为Acoo浏览器在用户代理中使用MSIE。请看错误的参数。我已经测试了所有IE浏览器,甚至Win8设备。您测试了IE11浏览器,但没有测试Acoo浏览器,并且Acoo浏览器在用户代理中使用MSIE,就像我说的那样,所以IEold也会检测Acoo浏览器为旧版本的IE,我确信Acoo browser在useragent中使用MSIE,因为我在带有Acoo浏览器的javascript测试站点上使用了navigator.useragent测试站点:您能给出一个更好的解决方案吗?您可以使用!navigator.useragent.Matchaco browser;&&navigator.useragent.match/MSIE/i?true:false,但这并不总是有效,因为Acoo浏览器并不总是有Acoo浏览器;在它的用户代理中,但实际上你不需要关心acoo浏览器在它的用户代理中有MSIE,因为acoo浏览器几乎是一样的。在我看来,这更健壮。当然,任何基于用户代理的东西都是毫无用处的。这比ActiveXObject有效。谢谢lot@tdakhla更新以过滤掉IE边缘。重要提示:否对于任何一个看起来似乎不起作用的答案,我都要检查仿真设置,我本打算将此答案标记为不正确,但后来我检查了仿真,发现一些intranet兼容性设置覆盖了显示模式,一旦从等式中去掉,此解决方案对我很有效。请确认d在非IE、IE8、9、10、Edge 14、15中为false。仅在IE11中为true。未在文档模式处于活动状态时进行测试。使用Browserstack进行测试。这篇Microsoft文章建议此解决方案可能不再工作。实际上,本文描述了我的方法工作的原因。如本文所述,尝试访问window.ActiveXObject将返回未定义的IE11现在和非Microsoft浏览器一样。在所有Microsoft浏览器中使用javascript in运算符的测试都返回true,因此IE11中严格来说两者都是如此。如果Microsoft对in运算符的行为进行更改,则是,此方法将断开。ActiveXObject in window在Edge中返回False。@Neo Edge不是IE,OP的问题是如何检测IE11@mastazi是的,但在这个答案中,有人提到ActiveXObject可以用来检测任何IE版本。Edge是否应该被称为IE版本是有争议的。微软当然不想称它为IE版本,但对于许多开发者来说,IE已经成为任何默认Microsoft浏览器的同义词。它也会返回IE10 Win 7上的trun“从用户代理获取IE版本”工作得很好!可以肯定的是,这将显示包括IE11在内的所有版本@
omer是的,因为它查找字符串MSIE或Trident;后者由IE 11及以上版本使用。因此,在微软改变其浏览器引擎的名称之前,它将适用于所有未来的IE版本。我相信新的Edge浏览器仍然使用Trident。这非常有效,谢谢@Beejor!我使用您的答案实现了一个简单的重定向到另一个页面:var ie=0;尝试{ie=navigator.userAgent.match/MSIE | Trident.*rv[:][0-9]+/[2];}如果ie!==0{location.href=../ie redirect/redirect.html;}@BernardV看起来不错!快速提示:如果您可以访问服务器,那么在脚本中检测用户代理可能会更好,因为用户不会注意到任何重定向/闪烁、更少的HTTP请求等。但在紧要关头,使用JS也可以。如果您使用的是chec的正则表达式,则可以添加i标志,使其不区分大小写,而不是.toLowerCase。也不需要.toString方法.My favorite-用于IE6-10和IE11。我在检查edge时也添加了此检测Firefox,因为这是IE 0我在哪里可以找到添加到新版本edge的新标准全局对象?我推断Math.acosh就是其中之一。@j4v1 Math.acosh仅在Microsoft Edge浏览器中受支持。这个方法对我来说已经不起作用了。过去它能正确地检测到IE11,但现在我的Internet Explorer版本11.1198.14393.0更新版本11.0.42 KB4018271在Windows 10企业版1607上运行,OS Build 14393.1198 Math似乎支持acosh方法。@Thijs在带有IE11 v 11.1198.14393.0的Windows 10教育中我测试成功。您应该根据ES6尝试另一个数学函数。@JamesPeter我在检查document.documentMode时找到了它。这似乎是检查IE11或Edge更可靠的属性。documentMode存在于IE11中,但不再存在于Edge中。
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 );
var isModernBrowser = ( !document.all || ( document.all && document.addEventListener ) ); 
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;
                }
        }
    }