| the complete webmaster | ||||
| tutorials | reviews | reference | ||
|
Server-side browser detection using ASPThe 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).
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 ' send some output to the web browser If all goes well, this short script will produce output something like this: Browser: IE Unfortunately, if you look at it in a different type of browser, you may well see output like this: Browser: Default 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 [Mozilla/4.5 * (Win95; I)] [Mozilla/4.5 * (Win98; I)] [Mozilla/4.5 * (WinNT; I)] 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 environmentOnce 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 if javascript = "True" then 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 More articles about Active Server Pages |
| write for us | about us | advertise |
Copyright 1997, 1998 A Big Lime. All rights reserved.