ASP.NET 四种判断浏览器类型(ENGLISH)
Browser Capabilities 组件
HttpBrowserCapabilities 类
Request.Browser 对象
----------------------------------------------------------------------------
----
Sniffing browser capabilities is nothing new to an experienced web developer
. Over time many different approaches have come around...using InStr() to se
e if a particular value, such as MSIE was in the HTTP_User_Agent value of th
e Request.ServerVariables collection, or using the Browser Capabilities comp
onent in ASP 3.0 to compare against. In ASP.NET you get the HttpBrowserCapab
ilities class, a new and improved Browser Capabilities component.
In this tutorial I will show you how to access and use the HttpBrowserCapabi
lities component. I am making the
assumption that you have done some browser sniffing in the past, and will be
brief in this tutorial, simply demonstrating
the new features.
HttpBrowserCapabilities
The HttpBrowserCapabilities class is derived from the HttpCapabilities class
, which is derived from the Object class.
Typical methods are available, such as ToString() and GetType(). For the mos
t part you should have to deal with these.
What you are most concerned with are the properties. The HttpBrowserCapabil
ities class exposes all of
the properties that the ASP 3.0 Browser Capabilities component exposed, and
a bit more. These properties
list the information about the browser and the client machine. This informat
ion enables you to write code to
selectively react to certain information.
Using the HttpBrowserCapabilities Class
In the past, browser sniffing could be a bit cumbersome. Take for example th
e code in listing 1.2
Listing 1.1 - Browser Sniffing the Ol' Fashion Way
<%
If InStr(Request.ServerVariables("HTTP_User_Agent"), "MSIE") Then
' Do something for Internet Explorer
Else
' Do something else
End If
%>
In the code in listing 1.2 you had to parse through the UserAgent string che
cking for a value. Not the most economical means of getting the information.
With the Browser Capabilities component, discovering browser information go
t better.
Listing 1.2 - Browser Sniffing with the Browser Capabilities Component
<%
Set objBC = Server.CreateObject("MSWC.BrowserType")
If objBC('browser') = "IE" Then
' Do something for Internet Explorer
Else
' Do something else
End If
%>
With the HttpBrowserCapabilities class you get the same ease of use as the B
rowser Capabilities component, with two advantages:
Its easier to use
It has more properties
HttpBrowserCapabilities ASP 3.0 Browser Capabilities Definition
ActiveXControls ActiveXControls Indicates
whether the client browser supports ActiveX controls.
AOL - Indicates whether the client is an AOL branded browser.£
BackgroundSounds backgroundsounds
Indicates whether the client supports background sounds.
Beta beta Indicates whether the client
browser is in beta.
Browser Browser The Browser string in the Request object's UserAgent propert
y.
CDF cdf Indicates whether the client browser supports Channel Definition For
mat (CDF) for web casting.
Cookies cookies Indicates whether the client browser supports cookies.
Crawler - Indicates whether the client
browser is a web-crawler/spider from a search-engine.£
Frames frames Indicates whether the client browser supports frames.
JavaApplets javaapplets Indicates whether
the client browser supports Java Applets.
JavaScript javascript Indicates whether the client browser supports JavaScri
pt.
MajorVersion - Returns the major version number of the client browser.
MinorVersion - Returns the minor version
number of the client browser.
Platform platform Returns the client platform.
Tables tables Indicates whether the client browser supports frames.
Type - Retunrs the name of the client
browser and the major version number.
VBScript vbscript Indicates whether the client browser supports VBScript.
Version version Returns the major version number and the minor version numbe
r.
Win16 - Indicates whether the client is a Win-16 based machine.
Win32 - Indicates whether the client is a Win-32 based machine.
As you can see from Table 1.1 you have a half dozen or so new properties you
can discover about the client.
Using the HttpBrowserCapabilities Class
You can gain access to the properties of the HttpBrowserCapabilities class i
n two ways, by setting an instance
of the class to the current Request.Browser object, or simply accessing the
Request.Browser object
directly. In Listing 1.3 I choose a CSS stylesheet for my Web Form after che
cking the Browser property
of the HttpBrowserCapabilities class.
Listing 1.3 - Browser Sniffing the HttpBrowserCapabilities Way
<html>
<head>
<script runat="server" language="vb">
Public Sub Page_Load(Source As Object, E As EventArgs)
Dim bc As HttpBrowserCapabilities = Request.Browser
If bc.Browser = "IE" Then
css1.Text = "<LINK rel='stylesheet' type='text/css' href='2000121901.css
'>"
Else
css1.Text = "<LINK rel='stylesheet' type='text/css' href='2000121901_nonie.c
ss'>"
End If
End Sub
</script>
<asp:Label id="css1" runat="server" />
</head>
<body>
<form runat="server" method="post">
</form>
</body>
</html>
The same result could be achieved by accessing the Request.Browser object di
rectly, as seen in listing 1.4.
Listing 1.4 - Browser Sniffing with the Request.Browser Object
<html>
<head>
<script runat="server" language="vb">
Public Sub Page_Load(Source As Object, E As EventArgs)
If Request.Browser.Browser = "IE" Then
css1.Text = "<LINK rel='stylesheet' type='text/css' href='2000121901.css
'>"
Else
css1.Text = "<LINK rel='stylesheet' type='text/css' href='2000121901_non
ie.css'>"
End If
End Sub
</script>
<asp:Label id="css1" runat="server" />
</head>
<body>
<form runat="server" method="post">
</form>
</body>
</html>
While it is a subtle difference between listings 1.3 and 1.4, there is a dif
ference. In listing 1.3 I declared a
variable for the HttpBrowserCapabilities class and instantiated it as the cu
rrent Request.Browser object. Then I
used the instance of the class to check the Browser property. In listing 1.
4 I avoided the creation of an
additional object by accessing the Request.Browser object directly. Either
way will get you the same result,
and if you are checking a lot of the properties of the HttpBrowserCapabiliti
es class, then a
variable instance of the class may be a better solution.
Download a Sample
With this brief tutorial I am providing a Web Form and two CSS stylesheets f
or you to download
(see the top of this tutorial for the link). The Web Form simply displays al
l of the properties of the
HttpBrowserCapabilities class and their values for the current browser. Addi
tionally the Web Form
has the code you saw above, to selectively include a CSS stylesheet based on
the browser.
from:asp学习网/title:ASP.NET 四种判断浏览器类型(ENGLISH)/ time:2007-6-13 23:09:55
本文主题ASP.NET,浏览器