The Browser Capabilities Component

The recent plethora of new Web technologies has created a dilemma for Web authors. Which technologies do you use to design your Web pages? VBScript (Visual Basic, Scripting Edition), ActiveX controls, Java applets, frames, and cascading style sheets are each supported in some browsers but not in others. So do you write applications that only the most technically capable browsers can use? Or do you write for the lowest common denominator?

The Browser Capabilities component helps you solve this problem. You can check a browser's capabilities and send HTML that you know the browser can handle.

When a browser requests a page from your Web server, it sends some information about itself in the HTTP user agent header of the request. The Browser Capabilities component compares this information to a list of browsers that it keeps in a file named browscap.ini. This file also has information about each browser's abilities. The Browser Capabilities component can then return information from the browscap.ini file for your use.

This component doesn't actually know anything more about a browser than you tell it in the browscap.ini file. It is strictly an engine for looking up already recorded capabilities. The more robust you make your browscap.ini file, the more information you'll have available in your application. Thankfully, Microsoft has provided a fairly extensive browscap.ini file. It covers browsers produced by Microsoft, Netscape, and Oracle. It is installed in different directories based on whether you're using Microsoft Windows 95 or Microsoft Windows NT and where your installation is. Therefore, do a search (Start|Find|Files or Folders) for browscap.ini.

The browscap.ini File

The browscap.ini file's layout is like any other .ini file, and it must be in the same directory as the component itself (the browscap.dll file). The default location for these files in IIS is C:\WINNT\system32\inetsrv.

Within the browscap.ini file, each browser definition has its own section and each section has any number of properties that you can set. You can also insert comments in the browscap.ini file, which are indicated by a semicolon (;). You can use comments anywhere in the file.

The HTTP user agent header within browscap.ini defines a browser's section. It must be enclosed in brackets, as shown here:

 [IE 4.0] 

You can use an asterisk as a wildcard in the HTTP user agent header. If the Browser Capabilities component cannot find the exact HTTP header, it tries to find the header by substituting any wildcards in the header. If more than one header matches the user agent header sent by the browser, the first matching header is used. For example,

 [Mozilla/2.0 *] 

matches

 [Mozilla/2.0 (compatible; MSIE 3.0; AK; Windows 95)] 

and

 [Mozilla/2.0 (compatible; MSIE 3.0B; Windows 95;1600,1280)] 

but it does not match

 [Mozilla/3.01b1 (Macintosh; I; PPC)] 

Within a section, you define any number of properties and their values. A property name must begin with a letter and cannot be longer than 255 characters. Below is a section from the browscap.ini file that comes with IIS 4.0.

 [IE 4.0] browser=IE Version=4.0 majorver=4 minorver=0 frames=TRUE tables=TRUE cookies=TRUE backgroundsounds=TRUE vbscript=TRUE javascript=TRUE javaapplets=TRUE ActiveXControls=TRUE Win16=False beta=False AK=False SK=False AOL=False crawler=False cdf=True 

A property can have three types of values. By default, a value is a string. You can make it an integer by prefacing it with a number sign (#). The majorver and minorver properties of Internet Explorer 4.0 are set to integers. You can set a property to a Boolean value by assigning it True or False.

There is one special property named parent whose value is the HTTP user agent header of another section from which you want to inherit properties, as shown here:

 ;;ie 4 beta 1 [Mozilla/4.0 (compatible; MSIE 4.0b1; Windows 95)] parent=IE 4.0 platform=Win95 beta=True cdf=False 

In this section, the parent property is set to IE 4.0. This section inherits all the properties from the IE 4.0 section. It also sets the platform property (which is not defined in its parent), sets the beta property to True, and sets the cdf property to False. The beta property is set to False in the IE 4.0 section, but the value is overridden in this section, as is the cdf value.

The browscap.ini file also supports a section for a default browser, shown below. You can use this to define what you consider the minimum base capabilities that you want to define in your application.

 [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 AK=False SK=False AOL=False beta=False Win16=False Crawler=False cdf=False AuthenticodeUpdate= 

The programmatic ID of the BrowserType object is MSWC.BrowserType, and the DLL that implements the component is named browscap.dll. The syntax for creating the object is

 <OBJECT RUNAT=server PROGID=MSWC.BrowserType id=oBrowsCap></OBJECT> 

This instantiates the BrowserType object and assigns it to the variable oBrowsCap.

The properties in the browscap.ini file define the properties for the BrowserType object. To add another property, you simply add the property to the .ini file. If the property is not found for a particular browser, it has a value of Unknown. If the browser itself is not found in the .ini file and no default browser section is defined, all the properties have a value of Unknown.

The BrowserType object has no methods.

Using the Browser Capabilities Component in an ASP Web page

The following code example displays the capabilities of the browser on an ASP Web page. The code is taken from the BrowserCapabilities.asp file in the Components Web project on the CD-ROM. Figure 17-6 shows the page's output.

 <%@ Language=VBScript %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> </HEAD> <BODY> <H2>Browser Capabilities Sample</H2> <HR> <OBJECT RUNAT=server PROGID=MSWC.BrowserType id=oBrowsCap> </OBJECT> <Table border=1> <tr> <td>Browser Name</td> <td> <%=oBrowsCap.Browser %> </td> <tr> <td>Browser Version</td>             <td> <%=oBrowsCap.Version %> </td> <tr> <td>Major Version</td> <td> <%=oBrowsCap.Majorver %> </td> <tr> <td>Minor Version</td> <td> <%=oBrowsCap.Minorver %> </td> <tr> <td>Frame Support</td> <td> <%=oBrowsCap.Frames %> </td> <tr> <td>Table Support</td> <td> <%=oBrowsCap.Tables %> </td> <tr> <td>Cookie Support</td> <td> <%=oBrowsCap.Cookies %> </td> <tr> <td>Background Sound Support</td> <td> <%=oBrowsCap.BackgroundSounds %> </td> <tr> <td>VBScript Support</td> <td> <%=oBrowsCap.VBScript %> </td> <tr> <td>JavaScript Support</td> <td> <%=oBrowsCap.JavaScript %> </td> </table> </BODY> </HTML> 

click to view at full size.

Figure 17-6. Output from the BrowserCapabilities.asp page.

There are many potential uses for the Browser Capabilities component. For example, if you want to play a background sound in your default or application home page and the browser is capable, you simply include an If…Then statement to check the browser's ability, as shown below:

 <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <TITLE>Document Title</TITLE> </HEAD> <BODY> <% If Session("oBrowsCap").Backgroundsounds Then %>     <BGSOUND SRC="sounds/jingle.wav"> <% End If %> <!-- Page Content Here --> </BODY> </HTML> 

Of course, this is not a particularly dramatic example of what you can do with this component. You can, for example, have an application written twice—once for frame-compatible browsers and once for browsers that are not frame-compatible. You can also use the Redirect method of the Response object to jump to the correct point from the default page based on the Browser Capabilities component.



Programming Microsoft Visual InterDev 6. 0
Programming Microsoft Visual InterDev 6.0
ISBN: 1572318147
EAN: 2147483647
Year: 2005
Pages: 143

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net