Further Reading
A very basic Ajax tutorial with ready-to-run examples is available at www.w3schools.
com/ajax/default.asp
. In Robin Nixon’s book Learning PHP, MySQL, JavaScript, and
CSS
you’ll find more explana tions about how Ajax works together with tips on using
XML instead of plain text for conveniently structuring data that you pass back to your
Ajax applications. If you’re inter ested in using the jQuery library (briefly introduced
in the next section) for programming your Ajax applications, you will proba bly want
to get David Sawyer McFarland’s book
JavaScript and jQuery. If you are up for more
technically prof ound explanation of the topic, you might want to loo k into
JavaScript:
The Definitive Guide
, a boo k by David Flanagan.
B.4 jQ uery
Currently by far the m ost popular and widely used JavaScript library is jQuery. There
are many reasons why you would decide to use jQuery in your own projec ts, and they
can all be brought under the same umbre lla with the motto “w rite less, do mo re.” If
nothing e lse, programmers like to use the jQuery library because it simp lifies mun-
dane everyday tasks and hides the differences between browsers, whic h have made
progr ammers reach f or bottles of aspirin from time immemorial. Also important is the
fact that jQuery is stable and well documented.
Getting jQuery
Technically, jQuery is simply a file containin g an a ssemblage of JavaScript co de,
which you include as you would any other external JavaScript file. T hat is, you use the
src attribute of a <script> HTML element. However, you still have two possibilities
regarding the library location: either yo u can download the jQuery file to your own
compute r or you c an simply link to one of the versions hosted on the web. Needless to
say, each of these two approaches has its advantages and downsides but I leave them
for y ou to discover on your own. For the sake of this section, we’re going to use the
jQuery library version hosted on the
jquery.com CDN (Content Distribution Network)
server:
<script src = "http://code.jquery.com/jquery-1.11.2.min.js">
</script>
The jQuery() Function
There’s one sin gle global function named jQuery() defined in the jQuery library.
Because this is the c entral query function for jQuer y, it is intensely used and the library
also defines the g lobal symbol $ as a shortcut alias for it. The jQuery() function
returns a jQuery o bject, which contains zero or more DOM elements and a bunch of
convenienc e methods for working with those DOM elements. Note that jQuery() is
not a con structor but a factory func tion and does therefo re n ot need the new operator
to create a jQuery object. Most jQuery methods return the same jQuery object that
they operate on. This allows for so-called method cha ining, which is q uite frequen t
in jQuery programming. Meth od chaining is a compact form of calling methods one
286 Appendix B. Ways to Continue