Capabilities vs. Quirks: When sniffing is OK
Written by on April 10th, 2007 in Ajax News.
Andrew Dupont has written a detailed report on Capabilities vs. Quirks: a look at browser sniffing.
The purists go on a rant if they ever see you touch navigator.userAgent, but Andrew gives reasons why Prototype has items such as:
-
-
Prototype.Browser = {
-
IE: !!(window.attachEvent && !window.opera),
-
Opera: !!window.opera,
-
WebKit: navigator.userAgent.indexOf(’AppleWebKit/’)> -1,
-
Gecko: navigator.userAgent.indexOf(’Gecko’)> -1 &&
-
navigator.userAgent.indexOf(’KHTML’) == -1
-
};
-
An example of a quirk that requires sniffing:
-
-
/* Force "Connection: close" for older Mozilla browsers to work
-
* around a bug where XMLHttpRequest sends an incorrect
-
* Content-length header. See Mozilla Bugzilla #246651.
-
*/
-
if (this.transport.overrideMimeType &&
-
(navigator.userAgent.match(/Gecko/(d{4})/) ||
-
[0,2005])[1] <2005)
-
headers[’Connection’] = ‘close’;
-
This makes my eyes water whenever I look at it: we’re parsing out a year from a user agent string. But what else is there to do? Even if it’s possible to detect this quirk on the client side (and I’m not sure it is), it’d involve sending a dummy Ajax request on page load. Checking navigator.userAgent doesn’t look so ridiculous after all.
Source: Ajaxian
Original Article: http://ajaxian.com/archives/capabilities-vs-quirks-when-sniffing-is-ok