<html>
<body>
<script type="text/javascript">
var browser=navigator.appName
var b_version=navigator.appVersion
var version=parseFloat(b_version)
document.write("浏览器名称:"+ browser)
document.write("<br />")
document.write("浏览器版本:"+ version)
</script>
</body>
</html>
[Web开发] 检测IE版本号的方法总结
检测浏览器(比如IE)的版本号码是Web 开发最常遇到的问题之一, 以下总结几种检测IE版本号码的方法:
通过Javascript解释浏览器的 User-Agent 字符串:
Javascript代码 1.function getInternetExplorerVersion() 2.// Returns the version of Internet Explorer or a -1 3.// (indicating the use of another browser). 4.{ 5. var rv = -1; // Return value assumes failure. 6. if (navigator.appName == 'Microsoft Internet Explorer') 7. { 8. var ua = navigator.userAgent; 9. var re = new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})"); 10. if (re.exec(ua) != null) 11. rv = parseFloat( RegExp.$1 ); 12. } 13. return rv; 14.} 15.function checkVersion() 16.{ 17. var msg = "You're not using Internet Explorer."; 18. var ver = getInternetExplorerVersion(); 19. 20. if ( ver > -1 ) 21. { 22. if ( ver >= 8.0 ) 23. msg = "You're using a recent copy of Internet Explorer." 24. else 25. msg = "You should upgrade your copy of Internet Explorer."; 26. } 27. alert( msg ); 28.} 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渲染引擎的的当前渲染模式:
Javascript代码 1.engine = null; 2.if (window.navigator.appName == "Microsoft Internet Explorer") 3.{ 4. // This is an IE browser. What mode is the engine in? 5. if (document.documentMode) // IE8 6. engine = document.documentMode; 7. else // IE 5-7 8. { 9. engine = 5; // Assume quirks mode unless proven otherwise 10. if (document.compatMode) 11. { 12. if (document.compatMode == "CSS1Compat") 13. engine = 7; // standards mode 14. } 15. } 16. // the engine variable now contains the document compatibility mode. 17.} 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 对象:
C-sharp代码 1.private float getInternetExplorerVersion() 2.{ 3. // Returns the version of Internet Explorer or a -1 4. // (indicating the use of another browser). 5. float rv = -1; 6. System.Web.HttpBrowserCapabilities browser = Request.Browser; 7. if (browser.Browser == "IE") 8. rv = (float)(browser.MajorVersion + browser.MinorVersion); 9. return rv; 10.} 11. 12.private void Page_Load(object sender, System.EventArgs e) 13.{ 14. string msg; 15. double ver = getInternetExplorerVersion(); 16. if (ver > 0.0) 17. { 18. if (ver >= 7.0) 19. msg = "You're using a recent version of Internet Explorer."; 20. else 21. msg = "You should upgrade your copy of Internet Explorer."; 22. } 23. else 24. msg = "You're not using Internet Explorer."; 25. 26. Label1.Text = msg; 27.} 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的扩展注释语句:
Xhtml代码 1.<!--[if gte IE 8]> 2.<p>You're using a recent version of Internet Explorer.</p> 3.<![endif]--> 4. 5.<!--[if lt IE 7]> 6.<p>Hm. You should upgrade your copy of Internet Explorer.</p> 7.<![endif]--> 8. 9.<![if !IE]> 10.<p>You're not using Internet Explorer.</p> 11.<![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文章提过
http://blog.csdn.net/WinGeek/archive/2009/02/08/3868150.aspx
http://blog.csdn.net/WinGeek/archive/2009/01/31/3855405.aspx
MSDN参考文章:(1), (2)