jQuery in Action
184
CHAPTER 6
jQuery utility functions
6.5 Summary
In this chapter we surveyed the features that jQuery provides outside of the meth-
ods that operate upon a wrapped set of matched
ods that operate upon a wrapped set of matched
DOM
elements. These included
an assortment of functions, as well as a set of flags, defined directly on the
jQuery
top-level name (as well as its
$
alias).
When we need to resort to browser detection to account for differences in
browser capabilites and operation, the
$.browser
set of flags lets us determine
within which browser family the page is being displayed. Browser detection
should be used only as a last resort when it's impossible to write the code in a
browser-independent fashion, and the preferred approach of object detection
can't be employed.
should be used only as a last resort when it's impossible to write the code in a
browser-independent fashion, and the preferred approach of object detection
can't be employed.
The
$.boxModel
flag tells us which of the two box models is being used to ren-
der the page, and the
$.styleFloat
flag lets us reference the style property of the
float
style in a browser-independent manner.
Recognizing that page authors may sometimes wish to use other libraries in
conjunction with jQuery, jQuery provides
$.noConflict()
, which allows other
libraries to use the
$
alias. After calling this function, all jQuery operations must
use the
jQuery
name rather than
$
.
$.trim()
exists to fill the gap left by the native JavaScript
String
class for trim-
ming whitespace from the beginning and end of string values.
jQuery also provides a set of functions that are useful for dealing with data sets
in arrays.
$.each()
makes it easy to traverse through every item in an array;
$.grep()
allows us to create new arrays by filtering through the data of a source
array using whatever filtering criteria we would like to use; and
$.map()
allows us
to easily apply our own transformations to a source array to produce a corre-
sponding new array with the transformed values.
sponding new array with the transformed values.
To merge objects, perhaps even to mimic a sort of inheritance scheme, jQuery
also provides the
$.extend()
function. This function allows us to unite the prop-
erties and any number of source objects into a target object.
And for those times when we want to load a script file dynamically, jQuery
defines
$.getScript()
, which can load and evaluate a script file at any point in
the execution of other page script.
With these additional tools safely tucked away in our toolbox, we're ready to
tackle how to add our own extensions to jQuery. Let's get to it in the next chapter.