CHAPTER 13
Browser Capabilities Component

One of the challenges of constructing a useful web site today is determining what
your users’ browsers can and cannot interpret in the form of content. Generally, if
you know the browser the current user is using, you know its base capabilities.
For example, you know that if the user is using Netscape Navigator, then he must
have an ActiveX plug-in to use ActiveX controls. However, what if the client is
using a less well known browser? Can you be sure the browser even supports
cookies?
In an attempt to help with this problem, Microsoft introduced the Browser Capabilities
component. You use the Browser Capabilities component to create a
BrowserType object. When you create a BrowserType object, the web server
retrieves the HTTP USER AGENT header sent by the client. Using this information,
the BrowserType object compares the information from this header to entries in a
special file (BrowsCap.ini ). If a match for the current client’s browser is found, the
BrowserType object determines all the properties for the specific browser. Your
scripts can then reference the properties of the BrowserType object to determine
the capabilities of the user’s browser. The following steps summarize this process:
1. The browser requests a page from the web server. That page contains an
instantiated BrowserType object. The browser sends an HTTP_USER_AGENT
request header. For example:
Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)
2. The BrowserType object looks this value up in BrowsCap.ini, retrieves a list
of capabilities for that browser, and loads them as properties of the Browser-
Type object itself.
3. The code can then use the properties of the BrowserType object to dynamically
determine the user’s browser’s capabilities.
If the BrowserType object does not find a match for the information from the
client’s USER AGENT HTTP header, all the properties of the BrowserType object
have the string value UNKNOWN.
Accessory Files/Required DLL Files 249
Browser Capabilities
Accessory Files/Required DLL Files
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Because browser capabilities change rapidly, Microsoft built into this control the
ability to add properties and new browser types by simply altering the accompanying
.INI file. You can even customize this file to reflect properties that may
apply only to your web site.
Accessory Files/Required DLL Files
BrowsCap.dll
The dynamic link library containing the Browser Capabilities component. It must
be registered on the web server before it can be instantiated in your web
applications.
BrowsCap.ini
The BrowsCap.ini file includes the HTTP USER AGENT header definitions and the
properties for the browsers defined by those headers. For more information about
the HTTP headers sent by the client, see the latest specification for the HTTP
protocol. You can add to the BrowsCap.ini file as many property definitions as
you wish. You also can define default values for each property definition.
Your BrowsCap.ini file must reside in the same physical directory as BrowsCap.dll.
This is the \WinNT\System32\inetsrv directory by default for Internet Information
Server 4.0.
The format of the BrowsCap.ini file must match the following:
[; comments]
[UserAgentHTTPHeader]
[Parent = strBrowserDefinition]
[strProperty1 = vntValue1]
...[additional code]
[strPropertyN = vntValueN]
; Default Browser Settings
[strDefaultProperty1 = vntDefaultValue1]
Browser Capabilities Summary
Properties
PropertyName (Customizable)
Collections
None
Methods
None
Events
None
250 Chapter 13 – Browser Capabilities Component
Accessory Files/Required DLL Files
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
...[additional code]
[strDefaultPropertyN = vntDefaultValueN]
The elements in the previous code break down as follows:
comments
You can add comments to the BrowsCap.ini file at any place in the file by
starting the comment line with a semicolon. These comments are ignored by
the BrowserType object.
UserAgentHTTPHeader
The USER AGENT HTTP header sent by the client. There is one
UserAgentHTTPHeader for each browser type defined in the file. For each
browser type thus defined, there is a series of property/value pairs for that
browser. Each UserAgentHTTPHeader entry in the BrowsCap.ini file must be
unique.
If you have several browser types that have the same property/value pairs but
slightly different HTTP headers, you can simplify your BrowsCap.ini file by
using wildcard characters. For example, the HTTP USER AGENT header entry:
[Mozilla/2.0 (compatible; MSIE 4.0;* Windows NT) ]
is functionally equivalent to all of the following:
[Mozilla/2.0 (compatible; MSIE 4.0; Windows NT) ]
[Mozilla/2.0 (compatible; MSIE 4.0; AK; Windows NT) ]
[Mozilla/2.0 (compatible; MSIE 4.0; AOL; Windows NT) ]
However, it is important to note that the BrowserType object will first try to
match the HTTP header string exactly before it attempts to match the entries
that use wildcards. This is important, because if you have both:
[Mozilla/2.0 (compatible; MSIE 4.0; AOL; Windows NT)]
and:
[Mozilla/2.0 (compatible; MSIE 4.0;* Windows NT)]
in the same BrowsCap.ini file and your user has the AOL version of Internet
Explorer 4.0, the first BrowsCap.ini entry will always used. If you add items to
the wildcard entry, then these entries will be ignored.
Also, if an HTTP header string matches more than one item in the BrowsCap.
ini file, the properties for the first matching entry are used for the Browser-
Type object.
strBrowserDefinition
An optional parameter that specifies a parent browser for the current browser.
This way, the current BrowserType object will inherit all the properties of the
parent browser’s entry in the BrowsCap.ini file. This makes the definitions of
newer versions of browsers in the BrowsCap.ini file easier, since they usually
support all the functionality of the parent browser. However, it is important to
note that if the newer version does not support some property of the parent
browser, you can explicitly set the property value for the newer browser, and
it will overwrite the inherited version of that same property.
Accessory Files/Required DLL Files 251
Browser Capabilities
Accessory Files/Required DLL Files
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
StrProperty(#)=vntValue(#)
A specific property and its value for a particular browser type. strProperty
is a property name, such as “ActiveXControls,” and the vntValue parameter
represents the value for that particular property. The vntValue parameter
value is a string by default. If the value represents an integer, it will be
prefixed by a pound sign (#). If the value represents a Boolean, the value will
be either True or False. The strProperty# name cannot contain spaces.
Each browser definition in the BrowsCap.ini file can contain as many or as
few property/value pairs as you need. For example, if your site only needs to
know if the client’s machine supports cookies, the BrowsCap.ini file could
contain only the single property definition
Cookies=True
You can also create your own special properties. For example, suppose your
company has a specialized version of Internet Explorer:
[Mozilla/2.0 (compatible; MSIE 4.0; MyCompany; Windows NT)]
This specialized version of IE has been customized so that the user cannot
right-click on images and save them. We’ll call this feature NoPicSave. You
could add the following to your BrowsCap.ini file:
[Mozilla/2.0 (compatible; MSIE 4.0; MyCompany; Windows NT)]
parent=IE 4.0
NoPicSave=TRUE
This code tells the BrowserType object that in addition to all the properties
found in the entry for IE 4.0, this customized version of the browser also has
the NoPicSave capability. Your code could then look for this entry and show
certain (perhaps sensitive) images to only those users with this feature.
Table 13-1 lists some possible property names and their definitions:
strDefaultProperty# / vntDefaultValue#
The [Default Browser Capability Settings] section of the BrowsCap.ini file
contains property/value pairs for all those properties for which you want to
assume a default value. The vntDefaultValue parameter value is either a
Table 13-1: Some Common Custom Properties
Property Description
Beta Whether the browser is a beta version
Browser The name of the browser
Cookies Whether the browser supports the use of cookies
Frames Whether the browser supports the use of frames
JavaApplets Whether the browser supports the use of Java applets
JavaScript Whether the browser supports the use of JavaScript
Platform The platform on which the client is running the browser
Tables Whether the browser supports the use of HTML tables
VbScript Whether the browser supports the use of VBScript
Version The browser’s version number
252 Chapter 13 – Browser Capabilities Component
Accessory Files/Required DLL Files
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Boolean (indicating whether a specific property is supported) or an integer. If
it is an integer, the value is prefixed with a pound sign. The
strDefaultProperty name cannot contain spaces.
The following is a (highly abbreviated) BrowsCap.ini file:
[Microsoft Pocket Internet Explorer/0.6]
browser=PIE
Version=1.0
majorver=1
minorver=0
frames=FALSE
tables=TRUE
cookies=FALSE
backgroundsounds=TRUE
vbscript=FALSE
javascript=FALSE
javaapplets=FALSE
ActiveXControls=FALSE
Win16=False
beta=False
AK=False
SK=False
AOL=False
platform=WinCE
[Netscape 4.00]
browser=Netscape
version=4.00
majorver=4
minorver=00
frames=TRUE
tables=TRUE
cookies=TRUE
backgroundsounds=FALSE
vbscript=FALSE
javascript=TRUE
javaapplets=TRUE
ActiveXControls=FALSE
beta=True
[Default Browser Capability Settings]
browser=Default
Version=0.0
majorver=#0
minorver=#0
frames=False
tables=True
cookies=False
backgroundsounds=False
vbscript=False
javascript=False
javaapplets=False
activexcontrols=False
Comments/Troubleshooting 253
Browser Capabilities
Comments/Troubleshooting
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
AK=False
SK=False
AOL=False
beta=False
Win16=False
Crawler=False
CDF=False
AuthenticodeUpdate=
This example file demonstrates all of the features of a BrowsCap.ini file. The
browser types described by this file are limited to two. A typical BrowsCap.ini file
could be as large as 30K in size.
Instantiating the Browser
Capabilities Component
To create an instance of the Browser Capabilities object, use the Server object’s
CreateObject method. The syntax for the CreateObject method is as follows:
Set objMyObject = Server.CreateObject(strProgId)
where:
• The objMyObject parameter represents the name of a Browser Capabilities
object.
• The strProgId parameter represents the programmatic ID (ProgID) for the
Browser Capabilities component:
MSWC.BrowserType
Example
<%
' The following code uses the Server object's
' CreateObject method to instantiate a Browser
' Capabilities object on the server.
Dim objBrowsType
Set objBrowsType = Server.CreateObject("MSWC.BrowserType")
%>
For more details on the use of the CreateObject method, see its entry in Chapter 8,
Server Object.
Comments/Troubleshooting
The Browser Capabilities component is fairly easy to use. The most important
issue with its use is to include the most up-to-date version of the BrowsCap.ini
file. Microsoft helps you here by providing an updated version free of charge on
its web site. To download the latest copy of the BrowsCap.ini file from Microsoft,
navigate to http://backoffice.microsoft.com/downtrial/moreinfo/bcf.asp and follow
the directions.
254 Chapter 13 – Browser Capabilities Component
Properties Reference
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Note, however, that you are not limited to the properties in the BrowsCap.ini file
from Microsoft. You can add you own custom properties and refer to them just as
you would to one of the standard properties.
Properties Reference
PropertyName (Customizable)
objBrowsType.strPropertyName
Determines the value of a given property of a BrowserType object. Note that these
properties represent values from the BrowsCap.ini file and are read-only. “Customizable”
means, as mentioned earlier, that you can add your own property names to
the BrowsCap.ini file.
Parameters
strPropertyName
The name of a standard or custom property in the BrowsCap.ini file. This
string value cannot contain spaces. If you attempt to retrieve the value of a
property that does not exist in the BrowsCap.ini file, the resulting value is the
string Unknown.
Example
<HTML>
<HEAD>
<TITLE>
Browser Capabilities
</TITLE>
</HEAD>
<BODY>
<%
' The following code example instantiates a BrowserType
' object and shows the user whether the browser
' supports various functions. Assume that the
' BrowsCap.ini file being used by the BrowserType
' object is the example file shown previously and that
' the user is using Netscape Navigator 4.0.
Dim objBrowsType
Set objBrowsType = Server.CreateObject("MSWC.BrowserType")
' The following properties will all evaluate to True
' and will result in the phrase
' "Support for property: TRUE":
%>
Support for frames: <%=objBrowsType.frames%><BR>
Support for tables: <%=objBrowsType.tables%><BR>
Support for cookies: <%=objBrowsType.cookies%><BR>
<%
PropertyName (Customizable) 255
Browser Capabilities
PropertyName (Customizable)
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
' However, the value of the following property will be
' the string "Unknown" resulting in the phrase
' '"Support for VRML: Unknown"
' because the vrml property is not defined in the preceding
' BrowsCap.ini file.
%>
Support for VRML: <%=objBrowsType.vrml%><BR>
</BODY>
</HTML>