如何检测用户是否正在运行IE 6?

3

我需要能够判断页面是否在IE 6中查看。如何使用JavaScript实现,同时忽略版本如7、8或其他浏览器?

2个回答

9

直接从消息来源得到的信息(只需一次谷歌搜索):

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  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;
}

6

+1:实际上我将其与mkoryak的答案结合使用了。 - Kredns
这是一种更好的方法 - 因为它在使用IE6引擎的其他浏览器上给出了“正确”的结果,并且不受用户代理欺骗的影响。更好的方法可能是使用特性(也称为对象检测)而不是用户代理检测 - http://www.quirksmode.org/js/support.html - Quentin
你知道,我提供的链接包含所有关于条件注释、特性检测甚至一些黑科技的信息。 - mkoryak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接