jQuery in Action
Using the jQuery flags
155
6.1.1 Detecting the user agent
Thankfully, almost blissfully, the jQuery commands that we've introduced so far
shield us from having to deal with browser differences, even in traditionally prob-
lematic areas like event handling. But when we're the ones writing these com-
mands (or other extensions), we often need to account for the differences in the
ways browsers operate so that the users of our extensions don't have to.
shield us from having to deal with browser differences, even in traditionally prob-
lematic areas like event handling. But when we're the ones writing these com-
mands (or other extensions), we often need to account for the differences in the
ways browsers operate so that the users of our extensions don't have to.
But before we dive into seeing how jQuery helps us in this regard, let's talk
about the concept of browser detection.
Why browser detection is heinous
OK
, maybe the word heinous is too strong, but unless it's absolutely necessary,
browser detection is a technique that should only be used when no other options
are available.
are available.
Browser detection might seem, at first, like a logical way to deal with browser
differences. After all, it's easy to say: "I know what the set of capabilities of
browser X are, so testing for the browser makes perfect sense, right?" But browser
detection is full of pitfalls and problems.
browser X are, so testing for the browser makes perfect sense, right?" But browser
detection is full of pitfalls and problems.
One of the major arguments against this technique is that the proliferation of
browsers, as well as varying levels of support within versions of the same browser,
makes this technique an unscalable approach to the problem.
makes this technique an unscalable approach to the problem.
You could be thinking, "Well, all I need to test for is Internet Explorer and
Firefox." But why would you exclude the growing number of Safari users? What
about Opera? Moreover, there are some niche, but not insignificant, browsers that
share capability profiles with the more popular browsers. Camino, for example,
uses the same technology as Firefox behind its Mac-friendly
about Opera? Moreover, there are some niche, but not insignificant, browsers that
share capability profiles with the more popular browsers. Camino, for example,
uses the same technology as Firefox behind its Mac-friendly
UI
. And OmniWeb uses
the same rendering engine as Safari.
There's no need to exclude support for these browsers, but it is a royal pain to
have to test for them. And that's not even considering differences between ver-
sions--
sions--
IE6
and
IE7
, for example.
Another argument against browser detection (or sniffing as it's sometimes
called) is that it's getting harder and harder to know who's who.
Browsers identify themselves by setting a request header known as the user
agent string. Parsing this string isn't for the faint-hearted. In addition, many
browsers now allow their users to spoof this string, so we can't even believe what it
tells us after we do parse it!
browsers now allow their users to spoof this string, so we can't even believe what it
tells us after we do parse it!
A JavaScript object named
navigator
gives us a partial glimpse into the user
agent information, but even it has browser differences. We almost need to do
browser detection in order to do browser detection!
browser detection in order to do browser detection!