| << 11.3.6- Using the ASP Content Linker to Generate a Table of Contents | Chapter11 | 11.5.0- Other Components >> |
The Browser Capabilities Component
One of the problems we face when creating all kinds of web pages, not just dynamic ones that use Active Server Pages, is deciding which of the range of tags and techniques we should be using. While it's great to be able to use all the latest features, such as Java applets, ActiveX controls, and the most recent HTML tags, we need to be aware that some visitors will be using browsers that don't support these. All they might see of our carefully crafted pages is a jumble of text, images, and – even worse – the code that makes them work.
We mentioned in Chapter 7 that you could use a Server variable, HTTP_USER_AGENT, to help detect which type of browser is viewing your page. However, life can be made much easier with the Browser Capabilities component, which translates from the USER_AGENT, into a list of features that a browser supports. Each feature can be tested for True (supported) or False (not supported) before using the feature. You can create an instance of the Browser Capabilities component as follows:
Set objBCap = Server.CreateObject("MSWC.BrowserType")
You can then use it by testing for True/False for support of dozens of features, for example:
If objBCap.Tables = "True" Then
' code that uses tables
Else
' code that avoids tables
End If
Recall that If...Then checks if the expression is "True". In the above code we wrote out a test, but since the contents of objBCap will be either "True" or "False" we don't have to write an equation. We can just use:
If objBCap.Tables Then
' code that uses tables
Else
' code that avoids tables
End If
The Browscap.ini File
The Browscap.ini file is the translation file that contains a list of browsers and their capabilities. There is also a default section of the file, which is used when the browser details don't match any of the ones more fully specified in the file. A default Browscap.ini comes with W2000 and is normally found in C:\WINNT\System32\InetServ, but you will probably want to get a more up to date version. You can find one that is maintained by Gary Keith as part of his Browser Capabilities Project at http://browsers.garykeith.com/downloads.asp. Normally there is no need for you to build a BrowsCap.ini; you just download and copy to your InetServ folder, but we will give you an overview of this file so you have an idea of how the BrowserType component will handle your code.
All of the entries in Browscap.ini are optional, however it's important that we always include the default section. If the browser in use doesn't match any in the Browscap.ini file, and no default browser settings have been specified, all the properties are set to "UNKNOWN".
; we can add comments anywhere, prefixed by a semicolon like this
[IE 5.0]
browser=IE
Version=5.0
majorver=5
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
MSN=False
CDF=True
DHTML=True
XML=True
[Default Browser Capability Settings]
browser=Default
Version=0.0
majorver=#0
...
The brackets of the [HTTPUserAgentHeader] line define the start of a section for a particular browser. Then each line defines a property that we want to inspect through the browser capabilities component, and its value for this particular browser. The properties list depends on the Browscap.ini, the above list for IE5 is about as complete as you will find. You can find what properties are available by opening your browscap.ini in any text editor and searching for the property of interest.
The Default section lists the properties and values that are used if the particular browser in use isn't listed in its own section, or if it is listed but not all the properties are supplied. In more sophisticated Browscap.ini files there are parent/child relationships so families of browsers can inherit characteristics.
Try It Out – Using the Browser Capabilities Component
Having grasped how the Browscap.ini file can translate a User_Agent into True/False/Unknown properties, it's time to actually see the Browser Capabilities component in use. This example checks to see whether or not the browser supports VBScript, and displays the appropriate message. This example can be modified to direct the user to different pages, depending on the response given by the browser.
1. Open up your favorite editor, type in the following code and save this page as BrowserCap.asp:
<% Option Explicit %>
<HTML>
<HEAD>
<TITLE> Browser Capabilities Component Example </TITLE>
</HEAD>
<BODY>
<%
Dim objBCap,blnVBScriptOK
Set objBCap = Server.CreateObject("MSWC.BrowserType")
blnVBScriptOK = objBCap.VBScript 'save the value in a variable
If blnVBScriptOK Then
Response.Write "This browser supports VBScript"
Else
Response.Write "This browser doesn't support VBScript"
End If
%>
</BODY>
</HTML>
2. Open up this page in Internet Explorer version 4.0 or higher:
|
|
3. Now view the same page in Netscape version 4.0 or higher:
|
|
How It Works
We first set up two variables for the object itself and a Boolean variable that will hold true or false for the ability to support VBScripting:
Dim objBCap,blnVBScriptOK
The first is then used to hold the instance of a BrowserType object:
Set objBCap = Server.CreateObject("MSWC.BrowserType")
Then we get the true/false property for VBScript and place it into the second variable:
blnVBScriptOK = objBCap.VBScript 'save the value in a variable
If the VBScript property holds the value True in browscap.ini, then we can assume that this browser supports VBScript:
If blnVBScriptOK Then
Response.Write "This browser supports VBScript"
Otherwise, we can assume the browser doesn't:
Else
Response.Write "This browser doesn't support VBScript"
End If
That's all there is to it. Of course, we can use the properties to do other things. One of the favorite techniques is to load a different index page for a site, depending on what features the browser supports. If our site has a set of pages using frames, and a different set using only simple text, we can check the browser's ability to display frames when it first hits our site, and redirect it to the appropriate index page.
| << 11.3.6- Using the ASP Content Linker to Generate a Table of Contents | Chapter11 | 11.5.0- Other Components >> |

RSS



