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:

  1. 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.
  2. KHTML — this will catch Safari, Omniweb and Konqueror. They mention Gecko, so if you need to treat them differently, check for KHTML first.
  3. Gecko — this will catch Mozilla, Firefox, Camino, Netscape 6+, etc.
  4. MSIE — this should Internet Explorer and anything else that uses its engine.
  5. bot, spider, crawler, or compatible — filter out robots and anything unknown.
  6. Mozillajust 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:

  1. Standards-compliant(ish) browsers: Gecko, Opera, KHTML
  2. MSIE
  3. 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.

Via Weblog about Markup & Style:

Dive Into Mark provides an excellent example of why a browser shouldn’t second-guess file types.

Safari content sniffing for XHTML

It’s a screenshot of Safari looking at a text document… but the first line mentions XHTML, so it’s decided that’s what it must be. I’ve had lots of trouble with Internet Explorer doing the same thing, and Safari’s been aiming at bug-for-bug compatibility in order to “break” as few sites as possible.

[Edit: After the source blog was taken down, I grabbed the screenshot from archive.org and uploaded it here.)

The new beta of OmniWeb uses Apple’s WebCore, the Konqueror-based code used to display pages in Safari.

What makes this so interesting is that OmniWeb was the first web browser designed to really work with OS X. IIRC MS really didn’t do much to IE except add the compatibility code and change the icons.

A lot has been going on in the web browser world over the last year and a half. Sure, the Browser War has been declared over, sure, Netscape has been declared dead time and time again, but IE has been stagnating ever since it “won.” Except for bug fixes, Microsoft hasn’t released a new version of IE for the Mac in over three years, or for Windows in almost two. They’ve declared they won’t be continuing IE on the Mac at all, and there’s talk of tying future Windows versions directly to the operating system, so that you can’t upgrade IE without upgrading Windows.

With the free availability of Gecko, the rendering engine that Mozilla and Netscape use to display pages, projects to develop web browers were springing up everywhere. People were trying out crazy new ideas like tabbed browsing (not the same thing as MDI), which proved popular enough it ended up back in Mozilla, where it became the most popular feature. Now every major browser but IE has it.

Now, the same thing looks to be happening with KHTML. What started out as a way for the KDE file manager to display web pages has become a very nice web browser, and since (like Gecko) it is available under an open source license, anyone is free to develop their own browser around it. Apple built Safari on top of it, making improvements where necessary, and made it easier for other Mac applications to use the same code. OmniWeb brings the number of KHTML browsers to at least three, and I suspect more will follow.

IE will probably continue to rule the statistics as long as Microsoft controls both it and Windows, and as long as Windows remains dominant on the desktop. But the innovation has moved elsewhere, and there are at least three other major browser types out there (Gecko-based, KHTML-based, and Opera) that can display pages very well, thank you.

With luck, this may begin moving us back to a platform-neutral Web, as it was originally intended.