5 Reasons Why Browser Sniffing Stinks

Browser sniffing is the act of detecting the web browser a visitor is using in order to serve version-specific pages, scripts, images, or other content. The technique was popular around 10 years ago because Microsoft and Netscape introduced their own technologies and web standards were in their infancy. In those dark days, it was fairly common to write two or more client-side scripts to implement identical functionality in different browsers.

Fortunately, the web has progressed. There is rarely a need to fork code for different devices, but Opera’s recent experience with version 10 indicates that browser sniffing remains a commonplace technique.

Technically, browser sniffing appears to be relatively straightforward. You obtain a user agent string using navigator.userAgent in JavaScript or the HTTP_USER_AGENT environment variable on the server and parse it accordingly. However, do not be complacent; browser sniffing should not be used!

The browser, version, rendering engine, OS, language, and information about media players and .NET frameworks can all be passed in various ways. Testing every combination is impossible; Opera 10 reports version 9.8 because some sniffers mistakenly believe it’s version 1.

2. There are many browsers to handle

Ten years ago, there were only three major browsers with one or two significant versions of each. Today, there are at least five mainstream browsers all with multiple versions running on different platforms. Ensuring you only target the right browser at the right time without affecting others is not easy.

3. Reduced scalability and increased maintenance

Different sets of code for different devices is harder to maintain and can never be future-proofed. In addition, your code could break every time a new browser or version is released. That is occurring increasingly often.

4. Browser masquerading

For many years, Opera pretended to be Internet Explorer by passing that browser’s user agent. This helped Opera users visit sites which normally refused non-IE browsers. It can still be done today.

5. It’s rarely required

Web standards have made browser sniffing far less necessary on the server side, unless you happen to be implementing a browser statistics application.

JavaScript does have several cross-browser compatibility issues, but object detection is normally a better solution. For example, IE7 and below do not have native support for XMLHttpRequest, the object essential for Ajax requests. However, an identical ActiveX object can be used instead – most libraries implement code such as:

A possible exception could be progressive enhancement aimed at specific browsers. For example, to support 24-bit PNGs in IE5.5 and IE6, browser sniffing could be used so images are only replaced if necessary. However, that is a workaround which will ultimately be removed once people stop using those browsers.

Cross-browser CSS is also possible – yes, even in IE6. Conditional stylesheets can help in extreme cases, although I personally avoid them because it still feels too much like browser sniffing!

Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.