Browser Detection using BrowserHawkBy Ryan S.
Well! I'm back again. You guys probably thought you wouldn't here from me again. Anyway, my article for today is going to be on.. browser detection. Now, with everyone getting all hyped up about dHTML (or they were), browser detection makes a lot of sense.
Some of you might be saying, "Well with dHTML, I can already do browser detection." That may be true, but more often than not you will generate some form of error on some browser more than you will display the right page.
For those who don't do dHTML, you might be saying, "Well. That's all good and swell for them, but what do I get out of it?" With browser detection, you can display the right page engineered for the browser. If any of you have been living under a rock for the past couple years, you would not know that Netscape and IE handle their HTML parsing differently. And because of such, layouts can appear different. Also, not all browsers support the same 'standards' as other browsers do. An example is the table background property.
Now that I've piqued your interest in browser detection, I'll get down to actually using it. Many of you are probably aware that IIS shipped with a file called
browscap.ini. This file lists browser "capabilities", so that you can customize your scripting. However, as some of you are aware, browscap.ini is not 100% accurate. There are other browser detection methods, but my personal preference is to use BrowserHawk, from cyScape. It's a wonderful component, but it is not totally free. However, because it offers a free trial, I'm going to focus on it for my discussion.
If you haven't already downloaded BrowserHawk, you can download it at the BrowserHawk Download Page. Once you have installed and setup the file, get ready to get rolling.
Now, the first example that I'm going to use is a simple way to redirect the user if they don't have frames. Sure, you could use <noframes>, but I'm sure your users get pretty mad seeing lame pages telling them they suck because they don't use a frames browser. Why not simply redirect them to a download page, so that they can download a branded browser? Or just redirect them to a frame-less page without them even knowing. Here's how it works:
<% Set myBrows = Server.CreateObject("cyScape.BrowserObj") if myBrows.Frames = False Then response.redirect "/download.asp" (or noFrames.asp or whatever file you want) End if %> <html> <frameset rows="1"> ... ... ... </frameset></html>
Or what if your whole site uses ActiveX, and you want to make sure they are using IE? Well, that's very simple.
<% set myBrows = Server.CreateObject("cyScape.BrowserObj") if myBrows.Browser <> "IE" Then response.redirect "noIE.asp" End If %> <html> ... ... </html>
As you can see, browser detection can be used for a variety of things. One of the really cool features I like about BrowserHawk is that is supports detection for a whole lot more browsers than browscap.ini. It also supports awesome features, such as dHTML and the like. Another use of browser detection would be to detect cookies, and thus determine whether you should be using session variables.