Using the HttpBrowserCapabilities Class...
There are any number of reasons to need to know certain capabilities of your user's browser. The HttpBrowserCapabilities Class provides you with many of the properties of the user's browser allowing you to adjust your presentation accordingly
There are still enough differences between Internet Explorer and Netscape that you may want to load different style sheets depending upon which browser your user is using. Does the browser support cookies? That may make a difference in how you handle authentication or other issues. Which version of the browser is being used? Does it support JavaScript, or VBScript? These and many other questions can be answered by use of the HttpBrowserCapabilities Class built in to .Net.
The properties of the class that are available to you are shown in the following table.
| Property | Description |
|---|---|
| ActiveXControls | A value indicating whether the browser supports ActiveX controls |
| AOL | A value indicating whether the client is an AOL browser |
| BackgroundSounds | A value indicating whether the browser supports background sounds |
| Beta | A value indicating whether the browser is a beta release |
| Browser | Gets the browser string that was transmitted in the User-Agent header |
| CDF | A value indicating whether the browser supports Channel Definition Format for web casting |
| Cookies | A value indicating whether the browser supports cookies |
| Crawler | A value indicating whether the browser is a web crawler search engine |
| EcmaScriptVersion | The version number of ECMA script that the browser supports |
| Frames | A value indicating whether the browser supports HTML frames |
| Item | Allows access to individual dictionary values. Further discussion is beyond the scope of this article |
| JavaApplets | A value indicating whether the browser supports Java applets |
| JavaScript | A value indicating whether the browser supports JavaScript |
| MajorVersion | The major version number of the browser |
| MinorVersion | The minor version number of the browser |
| MSDomVersion | The version of Microsoft XML Document Object Model that the browser supports |
| Platform | The name of the platform that the client uses |
| Tables | A value indicating whether the browser supports HTML tables |
| Type | The name and major version number of the browser |
| VBScript | A value indicating whether the browser supports VBScript |
| Version | The full major and minor version number of the browser |
| W3CdomVersion | The version of the World Wide Web Consortium XML Document Object Model that the browser supports |
| Win16 | A value indicating whether the client is a Win 16 based machine |
| Win32 | A value indicating whether the client is a Win 32 based machine |
You must, of course, instantiate the HttpBrowserCapabilities class using the Request.Browser object. The
instantiation takes the form
Dim bc As HttpBrowserCapabilities = Request.Browser
Below we present a little demonstration program which just lists the values of the properties of your browser if you run the demo program. The .aspx file presented below just contains a table presentation of label controls whose text values will be the corresponding property values of the HttpBrowserCapabilities object. A code-behind file shown further below instantiates the class and writes to the text properties of the various label controls.
|
<%@ Page Language="vb" Src="BrowserCaps.aspx.vb" Inherits="BrowserCaps"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>BrowserCaps</title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name=vs_defaultClientScript content="JavaScript"> <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5"> </head> <body> <div align="center"> <table border="1" cellspacing="0" cellpadding="4"> <tr> <td colspan="4" bgcolor="black" align="center"><font color="white"><b>Browser Capabilities</b></font></td> </tr> <tr> <td bgcolor="#EEEEEE">ActiveXControls</td> <td><asp:Label ID="lblActiveX" Runat="server" /></td> <td bgcolor="#EEEEEE">AOL</td> <td><asp:Label ID="lblAOL" Runat="server" /></td> </tr> <tr> <td bgcolor="#EEEEEE">BackgroundSounds</td> <td><asp:Label ID="lblBackgroundSounds" Runat="server" /></td> <td bgcolor="#EEEEEE">Beta</td> <td><asp:Label ID="lblBeta" Runat="server" /></td> </tr> <tr> <td bgcolor="#EEEEEE">Browser</td> <td><asp:Label ID="lblBrowser" Runat="server" /></td> <td bgcolor="#EEEEEE">CDF</td> <td><asp:Label ID="lblCDF" Runat="server" /></td> </tr> <tr> <td bgcolor="#EEEEEE">Cookies</td> <td><asp:Label ID="lblCookies" Runat="server" /></td> <td bgcolor="#EEEEEE">Crawler</td> <td><asp:Label ID="lblCrawler" Runat="server" /></td> </tr> <tr> <td bgcolor="#EEEEEE">EcmaScriptVersion</td> <td><asp:Label ID="lblEcmaScriptVersion" Runat="server" /></td> <td bgcolor="#EEEEEE">Frames</td> <td><asp:Label ID="lblFrames" Runat="server" /></td> </tr> <tr> <td bgcolor="#EEEEEE">JavaApplets</td> <td><asp:Label ID="lblJavaApplets" Runat="server" /></td> <td bgcolor="#EEEEEE">JavaScript</td> <td><asp:Label ID="lblJavaScript" Runat="server" /></td> </tr> <tr> <td bgcolor="#EEEEEE">MajorVersion</td> <td><asp:Label ID="lblMajorVersion" Runat="server" /></td> <td bgcolor="#EEEEEE">MinorVersion</td> <td><asp:Label ID="lblMinorVersion" Runat="server" /></td> </tr> <tr> <td bgcolor="#EEEEEE">MSDomVersion</td> <td><asp:Label ID="lblMSDomVersion" Runat="server" /></td> <td bgcolor="#EEEEEE">Platform</td> <td><asp:Label ID="lblPlatform" Runat="server" /></td> </tr> <tr> <td bgcolor="#EEEEEE">Tables</td> <td><asp:Label ID="lblTables" Runat="server" /></td> <td bgcolor="#EEEEEE">Type</td> <td><asp:Label ID="lblType" Runat="server" /></td> </tr> <tr> <td bgcolor="#EEEEEE">VBScript</td> <td><asp:Label ID="lblVBScript" Runat="server" /></td> <td bgcolor="#EEEEEE">Version</td> <td><asp:Label ID="lblVersion" Runat="server" /></td> </tr> <tr> <td bgcolor="#EEEEEE">W3CdomVersion</td> <td><asp:Label ID="lblW3CdomVersion" Runat="server" /></td> <td bgcolor="#EEEEEE">Win16</td> <td><asp:Label ID="lblWin16" Runat="server" /></td> </tr> <tr> <td bgcolor="#EEEEEE"> </td> <td> </td> <td bgcolor="#EEEEEE">Win32</td> <td><asp:Label ID="lblWin32" Runat="server" /></td> </tr> </table> </div> </body> </html> |
And finally for the code-behind file where the class is instantiated and its properties exposed. As you can see, the ToString() method is required on a few of the properties. All we are doing is displaying the properties. In actual use, you would probably be testing only a few of the properties and making logical choices in your code depending upon the values returned.
|
Imports System.Web.UI.WebControls Imports System.Web Public Class BrowserCaps : Inherits System.Web.UI.Page Protected lblActiveX as Label Protected lblAoL As Label Protected lblBackgroundSounds As Label Protected lblBeta As Label Protected lblBrowser As Label Protected lblCDF As label Protected lblCookies As Label Protected lblCrawler As Label Protected lblEcmaScriptVersion As Label Protected lblFrames As Label Protected lblJavaApplets As Label Protected lblJavaScript As Label Protected lblMajorVersion As Label Protected lblMinorVersion As Label Protected lblMSDomVersion As Label Protected lblPlatform As Label Protected lblTables As Label Protected lblType As Label Protected lblVBScript As Label Protected lblVersion As Label Protected lblW3CdomVersion As Label Protected lblWin16 As Label Protected lblWin32 As Label Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim bc As HttpBrowserCapabilities = Request.Browser lblActiveX.Text = bc.ActiveXControls lblAoL.Text = bc.AOL lblBackgroundSounds.Text = bc.BackgroundSounds lblBeta.Text = bc.Beta lblBrowser.Text = bc.Browser lblCDF.Text = bc.CDF lblCookies.Text = bc.Cookies lblCrawler.Text = bc.Crawler lblEcmaScriptVersion.Text = bc.EcmaScriptVersion.ToString() lblFrames.Text = bc.Frames lblJavaApplets.Text = bc.JavaApplets lblJavaScript.Text = bc.JavaScript lblMajorVersion.Text = bc.MajorVersion lblMinorVersion.Text = bc.MinorVersion lblMSDomVersion.Text = bc.MSDomVersion.ToString() lblPlatform.Text = bc.Platform lblTables.Text = bc.Tables lblType.Text = bc.Type lblVBScript.Text = bc.VBScript lblVersion.Text = bc.Version lblW3CdomVersion.Text = bc.W3CDomVersion.ToString() lblWin16.Text = bc.Win16 lblWin32.Text = bc.Win32 End Sub End Class |
You may download the code here.