Monday, October 29, 2007

BrowserCaps and other Browser

http://slingfive.com/pages/code/browserCaps/

ASP.Net -- its Request.Browser object is useful for easy browser detection. Request.Browser uses the section of the machine.config XML file (and the optional web.config XML file) to match the current browser's UA (User-Agent) string with its matching capabilities.

problem 1: just like ASP's browscap.ini before it, nobody official is updating this info. Microsoft pawns the job off on Cyscape.com, which doesn't care about doing the world a service (it's busy selling its competing BrowserHawk product instead). As a result, machine.config is already woefully out-of-date and unaware of newer browsers like Mozilla/Firefox, Netscape 7, Safari, and Konqueror, so it tells Request.Browser that they are "Netscape 5" (though Safari and Konqueror are wholly unrelated rendering engines).

problem 2: I went searching and searching for a newer BrowserCaps section updated by someone else. I found some possibilites (linked below), but I didn't like that they don't identify Netscape as Gecko (extrapolating the Netscape brand's marketing version from the real Gecko engine's version). Since Gecko is the rendering engine underneath, that's what we webdevs should care about.

problem 3: By default, Opera lies to web servers that it is Internet Explorer. It sends a UA string like "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera x.x".

solution: ...so I rolled my own BrowserCaps section. I swiped a starting point (also linked below), and warmed up my RegEx chops for the rest. Besides identifying Netscape 6+ and Mozilla/Firebird as Gecko, my update also fixes detection of Konqueror (aka KHTML) and Safari (aka AppleWebKit), and detects Opera as itself. Finally, it (now) also corrects the EcmaScriptVersion and TagWriter properties for Opera 6+.

files and demos
updated BrowserCaps section: with tabs or with spaces
sample page to test browserCaps update: code to copy or demo running on my web server
NEW: sample web.config file with updated browserCaps section: view or download

Installation

For both the web.config and machine.config files, the element goes inside the element (inside ), like so:

configuration>
system.web>
browserCaps>
...
/ browserCaps>
/ system.web>
/ configuration>


Warning: Machine.config is already full of other important code. Do not remove or replace any of it! Just insert the browserCaps contents (contents only!) into the end of the existing browserCaps section.

No comments: