最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect IE7 by the user agent string, leaving out newer IE versions running in quirks mode - Stack Overflow

programmeradmin3浏览0评论

I need to detect IE7 (and IE6) using the user agent string:

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:

Mozilla/4.0 (patible; 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 bine them into one?

I need to detect IE7 (and IE6) using the user agent string:

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:

Mozilla/4.0 (patible; 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 bine them into one?

Share Improve this question edited Jun 20, 2012 at 6:20 Jørgen asked May 15, 2012 at 8:24 JørgenJørgen 9,1409 gold badges51 silver badges68 bronze badges 4
  • 2 First question would of course be: Why do you "need" to detect IE7 by User Agent string? Feature detection should be preferred over browser detection, and then there are always Conditional Comments. – RoToRa Commented May 15, 2012 at 10:07
  • 2 I knew that one would e :P I need to address an issue which is not related to a specific feature. The issue is only applied to native IE7 installations (not in IE7+ in patibility mode). Finally, I can't use conditional ments, as the solution is distributed as a javascript widget. Thus as far as I can see, using user agent strings is the best option. – Jørgen Commented May 15, 2012 at 10:23
  • 1 Maybe you should ask a question about that specific issue. Is it a rendering issue or a JScript issue? If it's the latter, then JScripts Conditional Compilation statements may be usefull. – RoToRa Commented May 15, 2012 at 10:26
  • It is a javascript issue. This is a temporary solution, so I might just go with the double user agent matching. I'll check into the conditional pilation though :) Thanks! – Jørgen Commented May 15, 2012 at 12:54
Add a ment  | 

3 Answers 3

Reset to default 3

Assuming the Trident part always es after the MSIE part, you can use a lookahead:

/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:

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

UPDATE: second regex edited to include earlier versions of Trident too, e.g. Trident/4.0 in IE8, as well as potential later versions >= 10.

UPDATE2: Cleaned up RegEx's to be valid in javascript.

I am posting a JavaScript solution made along with the regex....

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

Maybe not a relevant but why not to try document&&document.documentMode?document.documentMode:variable where variable would be identifier for not being IE at all?

发布评论

评论列表(0)

  1. 暂无评论