How do I detect browsers without components?
We all know that to make a successful and attractive web site, we must strictly adhere to HTML 3.2 standards, avoiding all temptations to use any browser- or platform-specific tags and features.
Yeah, right. And 640k of RAM ought to be enough for anybody.
For the rest of us, we have to find innovative ways to present browser-specific content, yet still have it degrade gracefully for everyone else. This can be relatively straightforward for individual elements, such as ActiveX controls, cascading style sheets and even Java applets. But what if you want to present a certain paragraph only to IE4 users? or only let Netscape users see the contents of a specific <div> element? or make your body text 10pt for a Mac, and 9pt for a PC?
There is a way to do these things, without using COM objects, a database, or 200 tedious client-side document.write commands. All you need is a little familiarity with the user agent string of the browser(s) you wish to target.
Let's start with getting at that string in the first place. It is available in the server variable "http_user_agent" and can be accessed as follows (I convert this variable to lower case, in order to prevent ambiguity):
This code will produce a string similar to one of the following:
Be aware that there are literally thousands of unique user agent strings. Thankfully, the ones you will likely be most concerned with share common characteristics. Also, depending on the purpose of your discrimination, most can be grouped into similar categories.
Let's take DHTML for example. Say you want to have a headline that, when clicked on, presents an "abstract" and a link to more information (instead of linking the user right away). The code would look something like this:
In IE3 and NN3, the description and link will always be visible, and in Netscape 4 it will never be visible. In most cases the code above will generate a scripting error as well, which is not pretty. However, in IE4 or greater, the description will be hidden until the user clicks on the headline. Wouldn't it be nice if you had the ability to regulate that code, and ONLY insert the extra DHTML snippets for IE4 and IE5, preventing errors and other weird things in Netscape?
Here's what you can do, based on the code above. Since all IE4+ user agent strings (including AOL and NeoPlanet versions) are remarkably similar, you can search for "msie 4" or "msie 5" in the above strings. If this string is found, you know that your visitor is using IE4 or better, and so it is safe to include your DHTML code. For example:
If you find that certain script elements you use only work on certain platforms (e.g. Win32), you can add extra search elements into your discriminatory code, searching for key elements like "x11", "mac", "win95", "win98", or "winnt" -- the possibilities for this kind of browser detection are really only limited by your imagination and creativity. I use it almost religiously when writing any ASP application... from scripting functions, to adding CSS support, to deciding whether or not to tell people they're using an outdated browser.
If manual browser detection isn't for you, I strongly recommend you take a look at CyScape's BrowserHawk before you resign yourself to using Browscap. BrowserHawk is a very powerful server-side component which makes browser detection a breeze and, unlike Browscap, maintains itself as new browser versions are released. If you are using Browscap, however, make sure you keep it up to date as new browsers are released (see Article #2199).
Related ArticlesCan I code ISAPI filters / extensions with Visual Basic?
DLL: How do I avoid 'Permission Denied' when re-compiling?
How do I determine if a COM object is installed?
How do I determine if a VBScript-based object exists?
How do I generate PDF files from ASP?
How do I generate RTF documents from ASP?
How do I handle MD5 from ASP?
How do I pass server-side values to a client-side ActiveX control?
Should I close my objects and/or set them to nothing?
Should I store objects in session/application scope?
Should I use CreateObject or Server.CreateObject?
When does ASP release COM objects?
Where can I get a shopping cart for my web site?
Where can I get an updated version of browscap.ini?
Why do I get 'Server.CreateObject Access Error'?
Why do I get 8000401A errors?
Why do I get 80040111 errors?
Why do I get 80040112 / 8007045A errors?
Why do I get 800401F3 / 800A01AD errors?
Why do I get 80040460 errors?
Why do I get 80040514 errors?
Why do I get 8007000E errors?
Why do I get 8007007E / 8007007F errors?
Why do I get 80072EE2 errors?
Why do I get 80072EE5 errors?
Why do I get 800A0030 errors?
Why do I get 800A005B errors?
Why do I get 800A9C68 errors?
Why do I get 800C0007 errors?
Why does Browscap give me 'unknown' or tell me IE is Netscape?