Sometimes you want to know exactly what software people (or bots) are using to view your website. Sometimes all you want to know is which rendering engine’s quirks you need to cater to. To that end, I have here the ultra-simple browser detection algorithm. Just check the User-Agent string for each of the following words, in order:
Opera— they spoof IE by default, so check here first. If they ever change this to something else, you’ll be glad you started here.
KHTML— this will catch Safari, Omniweb and Konqueror. They mention Gecko, so if you need to treat them differently, check for KHTML first.
Gecko— this will catch Mozilla, Firefox, Camino, Netscape 6+, etc.
MSIE— this should Internet Explorer and anything else that uses its engine.
compatible— filter out robots and anything unknown.
Mozilla— just about everyone uses Mozilla in their UA string these days, but the rules above should filter most of them out and leave only old-school Netscape.
Of course, Mozilla, Opera and Safari have put much more effort than IE into following the standards, so most of the time you can write your code using the spec and just build in work-arounds. The way I look at it, there are three main categories:
- Standards-compliant(ish) browsers: Gecko, Opera, KHTML
- Everything else
I write for group 1, work-around for group 2 (and sometimes other browsers in group 1), and figure that group 3 (with the exception of Netscape 4, which has a tendency to do things like make links unclickable or hide entire chunks of the page if it doesn’t like your CSS) should at least be able to figure out how the text and graphics break down. It may not look perfect in randombrowser, but it should at least be comprehensible.