the complete webmaster
tutorials reviews reference
ASP
CGI
FrontPage
HTML
Java
JavaScript

Sponsored by El Scripto

Visit the Mortgage Loan Place for Home Loans and also click here to find VA Loans on our site.

Promote your website

home / articles / asp

Server-side browser detection using ASP

The differences between browser versions often create big problems for web authors, particularly those wanting to use recent developments such as ActiveX and Cascading Style Sheets. One possible solution is to have two sets of pages - one set optimized for newer browsers, and another with less features for older browsers.

Previously, many web developers detected different browsers by using JavaScript. This approach does have a fundamental flaw in that many browsers do not support this scripting language, or the user may have disabled the JavaScript feature. An alternative is to perform browser detection on the server. This has the advantage of working with all browsers, although some knowledge of server-side scripting is required.

All web browsers send an HTTP header - HTTP_USER_AGENT to the web server whenever they request an object on that server. The following simple ASP script will display the type of browser the page is being viewed with:

<%
user_agent = request.servervariables("HTTP_USER_AGENT")
response.write("The browser you are using is: " & user_agent)
%>

For example, my version of Netscape 4.5 returns Mozilla/4.5 [en] (Win95; I), whereas my Internet Explorer 4 returns Mozilla/4.0 (compatible; MSIE 4.01; Windows 95).

Although it wouldn't be particularly difficult to write an ASP script to parse these strings and find out the name and version of the browser, it would be time consuming to write such as script, and difficult to identify all the possible different HTTP_USER_AGENT strings that you may encounter.

Fortunately, a great deal of time is saved by using the Browser Capabilities ASP component supplied with Microsoft's Internet Information Server 3 and above (IIS), and with the Personal Web Server* for Windows 95/98 (PWS).

*Only versions of PWS supporting Active Server Pages will be supplied with this component.

An example of using this component is below:

<%
' create an instance of the Browser Capabilities component
Set browserdetect = Server.CreateObject("MSWC.BrowserType")

' find some properties of the browser being used to view this page
browser=browserdetect.Browser
version=browserdetect.Version
majorver=browserdetect.Majorver
minorver=browserdetect.Minorver
platform=browserdetect.Platform
frames=browserdetect.Frames
tables=browserdetect.Tables
cookies=browserdetect.Cookies
javascript=browserdetect.JavaScript

' send some output to the web browser
response.write ("Browser: " & browser & "<BR>")
response.write ("Version: " & version & "<BR>")
response.write ("Majorver: " & majorver & "<BR>")
response.write ("Minorver: " & minorver & "<BR>")
response.write ("Platform: " & platform & "<BR>")
response.write ("Supports frames: " & frames & "<BR>")
response.write ("Supports tables: " & tables & "<BR>")
response.write ("Supports cookies: " & cookies & "<BR>")
response.write ("Supports JavaScript: " & javascript & "<BR>")
%>

If all goes well, this short script will produce output something like this:

Browser: IE
Version: 4.0
Majorver: 4
Minorver: 01
Platform: Win95
Supports frames: True
Supports tables: True
Supports cookies: True
Supports JavaScript: True

Unfortunately, if you look at it in a different type of browser, you may well see output like this:

Browser: Default
Version: 0.0
Majorver: 0
Platform: Unknown
Supports frames: False
Supports tables: True
Supports cookies: False
Supports JavaScript: False

In this instance, the Browser Capabilities component has failed to recognize Netscape 4.5, so instead it assigns the values for the Default type of browser. To be on the safe side, it assumes an unknown browser doesn't support many of the more recent developments such as JavaScript or Cookies.

Obviously this isn't a very satisfactory state of things. Fortunately, help is at hand. The text file that contains the information about the various capabilities of different browsers is called browscap.ini, and resides somewhere on the server's hard disk [the exact location varies according to the type of web server you have installed - it's a good idea to do a search for it]. If you open this file in notepad, the top line should tell you when the file was last updated. The browscap.ini file normally supplied with IIS/PWS is quite old (mine is dated 16 June 1997), and detects comparatively few types of browsers. New types of browsers can be added quite easily. For example, if you add the following to the browscap.ini file, the component should be able to detect the Windows versions of Netscape 4.5:

;;Netscape 4.5
[Netscape 4.5]
browser=Netscape
version=4.5
majorver=#4
minorver=#5
frames=TRUE
tables=TRUE
cookies=TRUE
backgroundsounds=FALSE
vbscript=FALSE
javascript=TRUE
javaapplets=TRUE
ActiveXControls=FALSE
beta=False

[Mozilla/4.5 * (Win95; I)]
parent=Netscape 4.5
platform=Win95

[Mozilla/4.5 * (Win98; I)]
parent=Netscape 4.5
platform=Win98

[Mozilla/4.5 * (WinNT; I)]
parent=Netscape 4.5
platform=WinNT

Obviously, it would take a long time to add entries for all the various types of browser available. Fortunately an updated browscap.ini file may be downloaded free of charge from the Microsoft IIS website. Alternatively, take a look at the free browscap.ini file available on the cyScape, Inc. website. This file contains information on a wider range of browsers (not just those from Netscape and Microsoft), and is fully compatible with the Microsoft Browser Capabilities component.

Incidentally, cyScape also have a commercial product, BrowserHawk, which is able to detect a much wider range of browsers, as well as detecting browser features such as support for FileUpload, SSL, StyleSheets and DHTML. It also has many additional features, such as the automatic updating of the browser definition data, user agent logging, and more accurate detection of the latest browsers.

Using the Browser Capabilities component in a production environment

Once you have got the Browser Capabilities component working, you will be interested in using it in real life examples. One such example would be to redirect the user's browser according to the capabilities of their browser. For example, if you had a web page that contained a lot of JavaScript, you might like to use the following script on an intermediate page that would redirect the user to a page according to whether or not their browser supported JavaScript:

<%
' create an instance of the Browser Capabilities component
Set browserdetect = Server.CreateObject("MSWC.BrowserType")

' find out if the browser viewing this page supports JavaScript
javascript=browserdetect.JavaScript

if javascript = "True" then
    ' redirect browser to a page containing lots of JavaScript enhancements
    response.redirect("http://www.mydomain.com/javascript-enhanced-page.html")
else
   
' redirect browser to a basic page that doesn't contain JavaScript
   response.redirect("http://www.mydomain.com/standard-page.html")
end if

%>

Note that this code has to be added above the opening <HTML> tag on a page for it to work.

Another possible use could be to specify a different Cascading Style Sheet according to whether the web site visitor is using Netscape 4 or Internet Explorer 4.

Author: Brett Burridge
Date: 12/17/1998

More articles about Active Server Pages
More articles by Brett Burridge
Author Biography

Get more website traffic
write for us about us advertise

Copyright 1997, 1998 A Big Lime. All rights reserved.