[Web开发] 检测IE版本号的方法总结

检测IE版本方法
本文介绍多种检测Internet Explorer浏览器版本的方法,包括通过JavaScript解析User-Agent字符串、判断IE渲染引擎的渲染模式、利用ASP.NET的HttpBrowserCapabilities对象及使用HTML扩展注释语句。
检测浏览器(比如IE)的版本号码是Web 开发最常遇到的问题之一, 以下总结几种检测IE版本号码的方法:
通过Javascript解释浏览器的 User-Agent 字符串:

view plaincopy to clipboardprint?
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 );   
}  
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 );
}
通过Javascript判断IE渲染引擎的的当前渲染模式:
view plaincopy to clipboardprint?
engine = null;   
if (window.navigator.appName == "Microsoft Internet Explorer")   
{   
   // This is an IE browser. What mode is the engine in?   
   if (document.documentMode) // IE8   
      engine = document.documentMode;   
   else // IE 5-7   
   {   
      engine = 5; // Assume quirks mode unless proven otherwise   
      if (document.compatMode)   
      {   
         if (document.compatMode == "CSS1Compat")   
            engine = 7; // standards mode   
      }   
   }   
   // the engine variable now contains the document compatibility mode.   
}  
engine = null;
if (window.navigator.appName == "Microsoft Internet Explorer")
{
   // This is an IE browser. What mode is the engine in?
   if (document.documentMode) // IE8
      engine = document.documentMode;
   else // IE 5-7
   {
      engine = 5; // Assume quirks mode unless proven otherwise
      if (document.compatMode)
      {
         if (document.compatMode == "CSS1Compat")
            engine = 7; // standards mode
      }
   }
   // the engine variable now contains the document compatibility mode.
}
通过ASP.NET 的 HttpBrowserCapabilities 对象:

view plaincopy to clipboardprint?
private float getInternetExplorerVersion()   
{   
  // Returns the version of Internet Explorer or a -1   
  // (indicating the use of another browser).   
  float rv = -1;   
  System.Web.HttpBrowserCapabilities browser = Request.Browser;   
  if (browser.Browser == "IE")   
    rv = (float)(browser.MajorVersion + browser.MinorVersion);   
  return rv;   
}   
  
private void Page_Load(object sender, System.EventArgs e)   
{   
  string msg;   
  double ver = getInternetExplorerVersion();   
  if (ver > 0.0)   
  {   
    if (ver >= 7.0)    
      msg = "You're using a recent version of Internet Explorer.";   
    else  
      msg = "You should upgrade your copy of Internet Explorer.";   
  }    
  else  
    msg = "You're not using Internet Explorer.";   
  
  Label1.Text = msg;   
}  
private float getInternetExplorerVersion()
{
  // Returns the version of Internet Explorer or a -1
  // (indicating the use of another browser).
  float rv = -1;
  System.Web.HttpBrowserCapabilities browser = Request.Browser;
  if (browser.Browser == "IE")
    rv = (float)(browser.MajorVersion + browser.MinorVersion);
  return rv;
}
private void Page_Load(object sender, System.EventArgs e)
{
  string msg;
  double ver = getInternetExplorerVersion();
  if (ver > 0.0)
  {
    if (ver >= 7.0) 
      msg = "You're using a recent version of Internet Explorer.";
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  } 
  else
    msg = "You're not using Internet Explorer.";
  Label1.Text = msg;
}

通过HTML的扩展注释语句:

view plaincopy to clipboardprint?
<!--[if gte IE 8]>  
<p>You're using a recent version of Internet Explorer.</p>  
<![endif]-->  
  
<!--[if lt IE 7]>  
<p>Hm. You should upgrade your copy of Internet Explorer.</p>  
<![endif]-->  
  
<![if !IE]>  
<p>You're not using Internet Explorer.</p>  
<![endif]>  
<!--[if gte IE 8]>
<p>You're using a recent version of Internet Explorer.</p>
<![endif]-->
<!--[if lt IE 7]>
<p>Hm. You should upgrade your copy of Internet Explorer.</p>
<![endif]-->
<![if !IE]>
<p>You're not using Internet Explorer.</p>
<![endif]>
 
有些方法在之前的blog文章提过
MSDN参考文章:(1), (2)

 


 本文转自 陈本峰 51CTO博客,原文链接:http://blog.51cto.com/wingeek/273650,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值