简体   繁体   English

如何通过用户代理字符串检测IE7,省略以怪异模式运行的较新IE版本

[英]How to detect IE7 by the user agent string, leaving out newer IE versions running in quirks mode

I need to detect IE7 (and IE6) using the user agent string: 我需要使用用户代理字符串检测IE7(和IE6):

I have made the following regex: 我做了以下正则表达式:

navigator.userAgent.match(/MSIE [67]\./)

However, IE9 in quirks mode also matches the regex with the following user agent : 但是,怪异模式下的IE9也会将正则表达式与以下用户代理匹配:

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 7.1; Trident/5.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C)

I could make two regexes: 我可以制作两个正则表达式:

navigator.userAgent.match(/MSIE [67]\./) !== null 
  && navigator.userAgent.match(/Trident\/5\.0/) === null

Is there a way I can combine them into one? 有没有办法将它们组合成一个?

Assuming the Trident part always comes after the MSIE part, you can use a lookahead: 假设Trident部分始终位于MSIE部分之后,您可以使用前瞻:

/MSIE [67]\.(?!.*Trident[5]\.0)/

I'm not familiar with user agents strings, but I'm guessing that maybe IE10 in quirks mode could have a Trident version > 5, so you could change it to: 我不熟悉用户代理字符串,但我猜测在怪癖模式下的IE10可能有三叉戟版本> 5,因此您可以将其更改为:

/MSIE [67]\.(?!.*Trident[1-9])/

UPDATE: second regex edited to include earlier versions of Trident too, eg Trident/4.0 in IE8, as well as potential later versions >= 10. 更新:编辑第二个正则表达式以包括早期版本的Trident,例如IE8中的Trident / 4.0,以及潜在的更高版本> = 10。

UPDATE2: Cleaned up RegEx's to be valid in javascript. UPDATE2:清除RegEx在javascript中有效。

I am posting a JavaScript solution made along with the regex.... 我正在发布与正则表达式一起制作的JavaScript解决方案....

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;
}
function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    if ( ver >= 8.0 ) 
      msg = "You're using a recent copy of Internet Explorer."
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  }
  alert( msg );
}

This solution is actually given my Microsoft, you can refer to this link 这个解决方案实际上是我的微软,你可以参考这个链接

也许不是相关但为什么不尝试document&&document.documentMode?document.documentMode:variable ,其中variable将是不是IE的标识符?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM