如何检测IE11? [英] How to detect IE11?

本文介绍了如何检测IE11?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想要检测IE时,我使用这个代码:

When I want to detect IE I use this code:

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。如何检测它?

But IE11 is returning "You're not using Internet Explorer". How can I detect it?

推荐答案

IE11不再报告为 MSIE ,根据此更改列表,有意避免错误检测。

IE11 no longer reports as MSIE, according to this list of changes, it's intentional to avoid mis-detection.

如果您真的想知道它是IE检测<$如果 navigator.appName 返回 Netscape ,则用户代理中的c $ c> Trident / (未经测试的);

What you can do if you really want to know it's IE is to detect the Trident/ string in the user agent if navigator.appName returns Netscape, something like (the untested);

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

请注意,IE11(afaik)仍在预览中,用户代理可能发行前更改。

Note that IE11 (afaik) still is in preview, and the user agent may change before release.

这篇关于如何检测IE11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆