Feature detection != browser detection

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.

Comments

  1. Stuart Colville

    I agree but then what's the likelihood of another browser vendor implementing window.ActiveXObject in the case of IE, and window.opera in the case of Opera?

  2. Andrew Herron

    Stuart, what's to stop me from extending the window object with GreaseMonkey? Sure, it's not likely, but it is possible. Then again, so is user agent spoofing.

  3. Jeremy

    Whoever uses that code to determine if the browser's Firefox needs to shot. Ok, that's harsh, but you get the idea. I think the best way to determine Firefox is to not check for Firefox at all. Instead, check for Gecko and the Gecko version in the UA. That way, any browser that uses Gecko won't be shut out.

    I personally think checking for ActiveXObject is a pretty safe way of determining if the browser is IE. Granted, it doesn't do anything to determine the browser version, but if that's not an problem then its ok. Then again, I'm lazy ;)

  4. Nicholas C. Zakas

    You know, we used to say, "what's the likelihood that any new browser will support document.all?" yet both Opera and Firefox support it now. Feature detection is never the best way to determine which browser is being used. Feature detection should be used only for...wait for it...detecting features of a browser.

  5. José Jeria

    Bit offtopic, but about document.all in Firefox:

    It will only work in quirks mode and, most important, it is not detectable. So if(document.all) will not be true in Firefox.

  6. Nicholas C. Zakas

    That's a good point, Jose. It's not detectable in Firefox, but it is in Opera.

  7. zhanghao

    I support Nicholas c. Zakas.

Understanding JavaScript Promises E-book Cover

Demystify JavaScript promises with the e-book that explains not just concepts, but also real-world uses of promises.

Download the Free E-book!

The community edition of Understanding JavaScript Promises is a free download that arrives in minutes.