My take on Browser Sniffing vs Object Detection

Date: Sat Jan 24 2009 14:32:50 GMT-0600 (CST)
Some people are really hard core about object detection and others depend totally on browser detection. Personally, I think both sides are right. To me, it's the same argument as what language to write your code in. You use what you need to get the job done, period. I'm not claiming one is better than the other, I'm just stating that they are both useful when used properly. The first thing people say about User Agent sniffing is, "I can change the User Agent of my browser and things break.". Well my answer is, "And I care why?". I think that if you go through the trouble of changing the User Agent string on your browser, then you should not be surprised that things break. In fact, you should be expecting it. That would be like me changing the information on my license plates and then telling the officer: "You should have checked the VIN instead of the license plate, I didn't like what it said so I changed it". It's a stupid argument, get over it already! As for the object detection crew, you also need to do things right. If you are going to detect object's make sure that you are doing it correctly. Here's looking at you: document.all. While developing the YUI Rich Text Editor I learned that browser sniffing is, in deed, quite needed. Now, YUI does browser sniffing a little different. We try to detect the different rendering engines, so there is no firefox, mozilla, safari or chrome. It's gecko, webkit, ie, etc.. The main reason that sniffing is needed in the Editor is because of the execCommand method. All of the A-Grade browsers support the method. The method is there to detect, but they all don't do the same thing when executed. So how am I to tell what I am supposed to do? I have to rely on sniffing in order to make the browsers behave the way I want them to. Let's look at a another example. The method getBoundingClientRect was introduced in FireFox 3 and Opera 9.5 to match what was formerly an Interet Explorer only option. Now using object detection you would have done this: Now what about the few cases where Internet Explorer reports position off by 2 pixels? How are you going to deal with that? Browser sniffing, like this? Not to forget you object detecting guys, I have seen others do things like this (snagged from Mr. Zakas, who i quote "I [Nicholas] have to say right off the bat that hate this solution"): In my opinion, isn't this the same thing as browser sniffing? Aren't you checking something that you know is unique about a particular browser and leveraging it? Doesn't that also qualify as browser sniffing? If you are looking for some flaw in a browser to determine a course of action, wouldn't it be easier to just read the User Agent and work from there? You can process that info one time and use it everywhere. Now to be perfectly clear, I am not saying one is better than the other. I am simply stating that there are uses for both, just like there are uses for both PHP and Python, VIM and Emacs, Gnome and KDE, Harley and Honda, Mac and Linux (Windows doesn't count to me). You use what you need to get the job done. As with everything else make sure you make the right choice for the right job ;)