方法1:通过 BrowserInformation 类。
方法2:通过 HttpBrowserCapabilities 类。
方法3: 通过Javascript解释浏览器的 User-Agent 字符串。
方法4: 通过Javascript判断IE渲染引擎的的当前渲染模式。
方法5:通过HTML的扩展注释语句。
方法1:通过 BrowserInformation 类.
==========================================
适应于Silverlight程序。
提供有关浏览器的常规信息,例如名称、版本和操作系统。
下面的示例演示 BrowserInformation 类如何提供浏览器信息。
using System;
using System.Windows.Controls;
using System.Windows.Browser;
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
outputBlock.Text +=
"\nSilverlight can provide browser information:\n" +
"\nBrowser Name = " + HtmlPage.BrowserInformation.Name +
"\nBrowser Version = " + HtmlPage.BrowserInformation.BrowserVersion.ToString() +
"\nUserAgent = " + HtmlPage.BrowserInformation.UserAgent +
"\nPlatform = " + HtmlPage.BrowserInformation.Platform +
"\nCookiesEnabled = " + HtmlPage.BrowserInformation.CookiesEnabled.ToString() +
"\nProductName = " + HtmlPage.BrowserInformation.ProductName.ToString() +
"\nProductVersion = " + HtmlPage.BrowserInformation.ProductVersion.ToString();
}
}
IE8运行的结果:
Silverlight can provide browser information:
Browser Name = Microsoft Internet Explorer
Browser Version = 4.0
UserAgent = Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E)
Platform = Win32
CookiesEnabled = True
ProductName = MSIE
ProductVersion = 8.0
Safari运行的结果:
Silverlight can provide browser information:
Browser Name = Netscape
Browser Version = 5.0
UserAgent = Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50
Platform = Win32
CookiesEnabled = True
ProductName = Safari
ProductVersion = 534.50
方法2:通过 HttpBrowserCapabilities 类.
==========================================
适应于ASP.Net程序。
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = " + browser.AOL + "\n"
+ "Is Win16 = " + browser.Win16 + "\n"
+ "Is Win32 = " + browser.Win32 + "\n"
+ "Supports Frames = " + browser.Frames + "\n"
+ "Supports Tables = " + browser.Tables + "\n"
+ "Supports Cookies = " + browser.Cookies + "\n"
+ "Supports VBScript = " + browser.VBScript + "\n"
+ "Supports JavaScript = " + browser.JavaScript + "\n"
+ "Supports Java Applets = " + browser.JavaApplets + "\n"
+ "Supports ActiveX Controls = " + browser.ActiveXControls + "\n";
HttpBrowserCapabilities b = Request.Browser;
Response.Write("浏览器名称和版本号:" + b.Type + "<br>");
Response.Write("名称:" + b.Browser + "<br>");
Response.Write("版本:" + b.Version + "<br>");
Response.Write("操作平台:" + b.Platform + "<br>");
Response.Write("是否支持框架:" + b.Frames + "<br>");
Response.Write("是否支持表格:" + b.Tables + "<br>");
Response.Write("是否支持cookies:" + b.Cookies + "<br>");
浏览器名称和版本号:IE6
名称:IE
版本:6.0
操作平台:WinXP
是否支持框架:True
是否支持表格:True
是否支持cookies:True
if (Page.Request.Browser.EcmaScriptVersion.Major > 0 && Page.Request.Browser.W3CDomVersion.Major > 0)//判断是否支持脚本
{
Response.Write("浏览器名称与版本号:" + Page.Request.Browser.Type + "<br />");
Response.Write("浏览器版本号:" + Page.Request.Browser.Version + "<br />");
Response.Write("浏览器主版本号:" + Page.Request.Browser.MajorVersion + "<br />");
Response.Write("浏览器名称:" + Page.Request.Browser.Browser);
}
方法3: 通过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 );
}
方法4: 通过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.
}
方法5:通过HTML的扩展注释语句:
==================================
<!--[if !IE]-->
You're not using Internet Explorer.