I’m not sure what’s in the water, but lately I’ve been coming across a lot of confusion regarding the difference between feature detection and browser detection. There are a lot of people who don’t like using the user-agent string to determine the browser (I, of course, am a big proponent of it), favoring instead the use of feature detection. As I’ve stated before, there is a time and a place to use each technique, but there is no time when you should confuse the two. For example, the following is highly undesirable:

var isIE = document.uniqueID && window.ActiveXObject;
var isFirefox = typeof document.getBoxObjectFor == "function";

This the complete and utter wrong way of doing both feature and browser detection. The code above makes assumptions about the browser based on the availability of certain features. The gaping hole in this logic is that other browsers may decide to implement such features in the future, which renders the code useless.

Remember: feature detection is fine when you’re trying to figure out whether to use that feature or not. Feature detection is not the way to determine which browser is being used.

Disclaimer: Any viewpoints and opinions expressed in this article are those of Nicholas C. Zakas and do not, in any way, reflect those of my employer, my colleagues, Wrox Publishing, O'Reilly Publishing, or anyone else. I speak only for myself, not for them.

Both comments and pings are currently closed.