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.
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.




7 Comments
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?
Stuart Colville on January 15th, 2007 at 4:04 am
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.
Andrew Herron on January 15th, 2007 at 9:13 am
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
Jeremy on January 15th, 2007 at 9:19 am
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.
Nicholas C. Zakas on January 15th, 2007 at 1:52 pm
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.
José Jeria on January 16th, 2007 at 2:32 am
That’s a good point, Jose. It’s not detectable in Firefox, but it is in Opera.
Nicholas C. Zakas on January 16th, 2007 at 1:12 pm
I support Nicholas c. Zakas.
zhanghao on February 10th, 2007 at 11:43 am
Comments are automatically closed after 14 days.