[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,如需转载请自行联系原作者

相关文章
|
16天前
|
算法 Java Go
【GoGin】(1)上手Go Gin 基于Go语言开发的Web框架,本文介绍了各种路由的配置信息;包含各场景下请求参数的基本传入接收
gin 框架中采用的路优酷是基于httprouter做的是一个高性能的 HTTP 请求路由器,适用于 Go 语言。它的设计目标是提供高效的路由匹配和低内存占用,特别适合需要高性能和简单路由的应用场景。
118 4
|
4月前
|
缓存 JavaScript 前端开发
鸿蒙5开发宝藏案例分享---Web开发优化案例分享
本文深入解读鸿蒙官方文档中的 `ArkWeb` 性能优化技巧,从预启动进程到预渲染,涵盖预下载、预连接、预取POST等八大优化策略。通过代码示例详解如何提升Web页面加载速度,助你打造流畅的HarmonyOS应用体验。内容实用,按需选用,让H5页面快到飞起!
|
4月前
|
JavaScript 前端开发 API
鸿蒙5开发宝藏案例分享---Web加载时延优化解析
本文深入解析了鸿蒙开发中Web加载完成时延的优化技巧,结合官方案例与实际代码,助你提升性能。核心内容包括:使用DevEco Profiler和DevTools定位瓶颈、四大优化方向(资源合并、接口预取、图片懒加载、任务拆解)及高频手段总结。同时提供性能优化黄金准则,如首屏资源控制在300KB内、关键接口响应≤200ms等,帮助开发者实现丝般流畅体验。
|
前端开发 JavaScript Shell
鸿蒙5开发宝藏案例分享---Web页面内点击响应时延分析
本文为鸿蒙开发者整理了Web性能优化的实战案例解析,结合官方文档深度扩展。内容涵盖点击响应时延核心指标(≤100ms)、性能分析工具链(如DevTools时间线、ArkUI Trace抓取)以及高频优化场景,包括递归函数优化、网络请求阻塞解决方案和setTimeout滥用问题等。同时提供进阶技巧,如首帧加速、透明动画陷阱规避及Web组件初始化加速,并通过优化前后Trace对比展示成果。最后总结了快速定位问题的方法与开发建议,助力开发者提升Web应用性能。
|
1月前
|
安全 Linux PHP
Web渗透-命令执行漏洞-及常见靶场检测实战
命令执行漏洞(RCE)指应用程序调用系统命令时,用户可控制输入参数,导致恶意命令被拼接执行,从而危害系统安全。常见于PHP的system、exec等函数。攻击者可通过命令连接符在目标系统上执行任意命令,造成数据泄露或服务瘫痪。漏洞成因包括代码层过滤不严、第三方组件缺陷等。可通过参数过滤、最小权限运行等方式防御。本文还介绍了绕过方式、靶场测试及复现过程。
454 0
|
4月前
|
JSON 开发框架 自然语言处理
【HarmonyOS Next之旅】基于ArkTS开发(三) -> 兼容JS的类Web开发(三)
本文主要介绍了应用开发中的三大核心内容:生命周期管理、资源限定与访问以及多语言支持。在生命周期部分,详细说明了应用和页面的生命周期函数及其触发时机,帮助开发者更好地掌控应用状态变化。资源限定与访问章节,则聚焦于资源限定词的定义、命名规则及匹配逻辑,并阐述了如何通过 `$r` 引用 JS 模块内的资源。最后,多语言支持部分讲解了如何通过 JSON 文件定义多语言资源,使用 `$t` 和 `$tc` 方法实现简单格式化与单复数格式化,为全球化应用提供便利。
221 104
|
4月前
|
JavaScript 前端开发 API
【HarmonyOS Next之旅】基于ArkTS开发(三) -> 兼容JS的类Web开发(二)
本文介绍了HarmonyOS应用开发中的HML、CSS和JS语法。HML作为标记语言,支持数据绑定、事件处理、列表渲染等功能;CSS用于样式定义,涵盖尺寸单位、样式导入、选择器及伪类等特性;JS实现业务逻辑,包括ES6语法支持、对象属性、数据方法及事件处理。通过具体代码示例,详细解析了页面构建与交互的实现方式,为开发者提供全面的技术指导。
238 104
|
4月前
|
开发框架 编解码 JavaScript
【HarmonyOS Next之旅】基于ArkTS开发(三) -> 兼容JS的类Web开发(一)
该文档详细介绍了一个兼容JS的类Web开发范式的方舟开发框架,涵盖概述、文件组织、js标签配置及app.js等内容。框架采用HML、CSS、JavaScript三段式开发方式,支持单向数据绑定,适合中小型应用开发。文件组织部分说明了目录结构、访问规则和媒体文件格式;js标签配置包括实例名称、页面路由和窗口样式信息;app.js则描述了应用生命周期与对象管理。整体内容旨在帮助开发者快速构建基于方舟框架的应用程序。
222 102
|
6月前
|
移动开发 前端开发 JavaScript
H5 页面与 Web 页面的制作方法
H5页面制作利用HTML5、CSS3和JavaScript技术,结合H5编辑器或框架(如Adobe Dreamweaver、Ionic),注重移动设备兼容性与响应式布局。Web页面制作则基于传统HTML、CSS和JavaScript,借助文本编辑器或IDE完成开发。两者区别在于技术版本、交互性和浏览器支持:H5更互动、现代,但可能不兼容旧浏览器;Web页面更静态、兼容性广。根据需求选择:高交互选H5,广泛兼容选Web。
886 6
|
5月前
|
Web App开发 前端开发 JavaScript
鸿蒙5开发宝藏案例分享---Web适配一多开发实践
这是一份实用的鸿蒙Web多设备适配开发指南,针对开发者在不同屏幕尺寸下的布局难题提供了解决方案。文章通过三大法宝(相对单位、媒体查询和窗口监听)详细介绍如何实现智能适配,并提供了多个实战案例,如宫格布局、对话框变形和自适应轮播图等。此外,还分享了调试技巧及工具推荐,帮助开发者快速上手并优化性能。最后鼓励读者实践探索,并提示更多官方资源等待发现。

热门文章

最新文章