jQuery in Action
158
CHAPTER 6
jQuery utility functions
Note that these flags don't attempt to identify the specific browser that's being
used; jQuery classifies a user agent based upon which family of browsers it belongs
to. Browsers within each family will sport the same sets of characteristics; specific
browser identification should not be necessary.
used; jQuery classifies a user agent based upon which family of browsers it belongs
to. Browsers within each family will sport the same sets of characteristics; specific
browser identification should not be necessary.
The vast majority of commonly used, modern browsers will fall into one of
these four browser families.
The
version
property deserves special notice because it's not as handy as we
might think. The value set into this property isn't the version of the browser (as
we might initially believe) but the version of the browser's rendering engine. For
example, when executed within Firefox 2.0.0.2, the reported version is
we might initially believe) but the version of the browser's rendering engine. For
example, when executed within Firefox 2.0.0.2, the reported version is
1.8.1.6
--
the version of the Gecko rendering engine. This value is handy for distinguishing
between
between
IE6
and
IE7
, containing
6.0
and
7.0
respectively.
We mentioned earlier that there are times when we can't fall back on object
detection and must resort to browser detection. One example of such a situation
is when the difference between browsers isn't that they present different object
classes or different methods but that the parameters passed to a method are inter-
preted differently across the browser implementations. In such a case, there's no
object to perform detection on.
is when the difference between browsers isn't that they present different object
classes or different methods but that the parameters passed to a method are inter-
preted differently across the browser implementations. In such a case, there's no
object to perform detection on.
Let's look at the
add()
method to the
<select>
elements. It's defined as
the following:
selectElement.add(element,before)
In this method, the first parameter identifies an
<option>
or
<optgroup>
element
to add to the
<select>
element and the second identifies the existing
<option>
(or
<optgroup>
) that the new element is to be placed before. In standards-compliant
browsers, this second parameter is a reference to the existing element; in Internet
Explorer, it's the ordinal index of the existing element.
Explorer, it's the ordinal index of the existing element.
Flag syntax: $.browser
$.browser
Defines a set of flags that can be used to discover to which broad set of browser families the
current user agent belongs. These flags are as follows:
current user agent belongs. These flags are as follows:
msie
--Set to true if the user agent header identifies the browser as Microsoft Internet
Explorer.
mozilla
--Set to true if the user agent header identifies the browser as any Mozilla-
based browser. This includes browsers such as Firefox, Camino, and Netscape.
safari
--Set to true if the user agent header identifies the browser as Safari or any
Safari-based browser such as OmniWeb.
opera
--Set to true if the user agent header identifies the browser as Opera.
version
--Set to the version number of the rendering engine for the browser.