博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Web开发] 检测IE版本号的方法总结
阅读量:6886 次
发布时间:2019-06-27

本文共 4509 字,大约阅读时间需要 15 分钟。

检测浏览器(比如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,如需转载请自行联系原作者
你可能感兴趣的文章
Python3+pyshark捕获数据包并保存为文件
查看>>
Unable to launch the IIS Express Web server
查看>>
怎样使用EOS.JS的API
查看>>
Spring Boot项目配置RabbitMQ集群
查看>>
口语详解|为什么“how to say”是错的?
查看>>
前端工程化系列[05] Yeoman脚手架使用入门
查看>>
MySQL必会的28条经典查询
查看>>
原生JS替代jQuery的各种方法汇总
查看>>
彻底删除Cygwin
查看>>
一次单核CPU占用过高问题的处理
查看>>
js常用加解密函数汇总
查看>>
排查nginx、tomcat内存和服务器负载之后
查看>>
[转]比特币测试链——Testnet介绍
查看>>
如何让自己的电脑发布ASP http://jingyan.baidu.com/article/19192ad853224ce53f570748.html
查看>>
SQL基础
查看>>
String是基本的数据类型吗?
查看>>
程序员的八个级别
查看>>
为什么互联网产品的成功率这么低
查看>>
Android动画学习笔记-Android Animation
查看>>
sql2003安装sql2005企业版
查看>>