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

简介:
检测浏览器(比如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月前
|
安全 Linux PHP
Web渗透-命令执行漏洞-及常见靶场检测实战
命令执行漏洞(RCE)指应用程序调用系统命令时,用户可控制输入参数,导致恶意命令被拼接执行,从而危害系统安全。常见于PHP的system、exec等函数。攻击者可通过命令连接符在目标系统上执行任意命令,造成数据泄露或服务瘫痪。漏洞成因包括代码层过滤不严、第三方组件缺陷等。可通过参数过滤、最小权限运行等方式防御。本文还介绍了绕过方式、靶场测试及复现过程。
2026 0
|
移动开发 前端开发 JavaScript
H5 页面与 Web 页面的制作方法
H5页面制作利用HTML5、CSS3和JavaScript技术,结合H5编辑器或框架(如Adobe Dreamweaver、Ionic),注重移动设备兼容性与响应式布局。Web页面制作则基于传统HTML、CSS和JavaScript,借助文本编辑器或IDE完成开发。两者区别在于技术版本、交互性和浏览器支持:H5更互动、现代,但可能不兼容旧浏览器;Web页面更静态、兼容性广。根据需求选择:高交互选H5,广泛兼容选Web。
1935 6
|
存储 JSON JavaScript
WEB CAD插件通过上下文对象MxPluginContext修改UI界面的方法
本文介绍了如何使用MxPluginContext动态控制MxCAD项目的UI界面。通过该上下文对象,开发者可以灵活设置UI配置,如控制操作栏显隐、编辑按钮、添加侧边栏等。具体方法包括调用`getUiConfig()`获取并修改`mxUiConfig.json`中的属性,实现界面的定制化。此外,还提供了控制命令行聚焦的功能,解决输入框焦点锁定问题。详细代码示例和效果对比图展示了具体实现步骤,帮助开发者更好地适配项目需求。
|
SQL 存储 安全
Web 常见攻击方式及防御方法
【10月更文挑战第25天】Web 安全是一个复杂而重要的领域,攻击者不断寻找新的攻击方法,我们需要不断加强防御措施,提高安全意识,以保障 Web 应用的安全运行。通过采取多种防御手段的综合运用,我们可以有效地降低 Web 攻击的风险,保护用户的信息和财产安全。同时,随着技术的不断发展,我们也需要持续关注和研究新的安全威胁和防御方法,以应对不断变化的安全形势。
1869 56
|
存储 安全 Go
Web安全基础:防范XSS与CSRF攻击的方法
【10月更文挑战第25天】Web安全是互联网应用开发中的重要环节。本文通过具体案例分析了跨站脚本攻击(XSS)和跨站请求伪造(CSRF)的原理及防范方法,包括服务器端数据过滤、使用Content Security Policy (CSP)、添加CSRF令牌等措施,帮助开发者构建更安全的Web应用。
769 3
|
存储 前端开发 API
前端开发中,Web Storage的存储数据的方法localstorage和sessionStorage的使用及区别
前端开发中,Web Storage的存储数据的方法localstorage和sessionStorage的使用及区别
674 0
|
前端开发 JavaScript
掌握微前端架构:构建现代Web应用的新方法
本文介绍了微前端架构的概念及其在现代Web应用开发中的优势与实施方法。微前端架构通过将应用拆分成独立模块,提升了开发效率和灵活性。其核心优势包括技术栈灵活性、独立部署、团队协作及易于维护。文章详细阐述了定义边界、选择框架、管理状态和通信等关键步骤,并讨论了状态同步、样式隔离及安全性等挑战。微前端架构有望成为未来Web开发的重要趋势。
WEB端在线CAD中实现测量圆、测量面积的方法
实现在线CAD中测量圆和测量面积的功能开发,用户点击目标圆对象将自动标记出这个圆的半径、面积值和周长值,同时可以自定义选择标注文字的位置,测量圆功能能够快速掌握目标圆对象的数据信息,方便统计工程量。
WEB端在线CAD中实现测量圆、测量面积的方法