Further Reading
If you want to read more abou t Web Storage, there’s quite an elaborate and beginner-
friendly coverage of the topic inside the b ook titled
HTML5 by Matthew MacDonald.
You may also want to look inside
JavaScript: The Definitive Guide by David Flanagan
for more elaborate coverage of the subject. Both books also cover offline applications
quite thoroughly.
Online, there’s a decen t article ava ilable at
www.sitep oint.com/an-overview-of-the-web-
storage-api
. For more comprehensive reading, though, you mig ht want to dig into
developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API.
B.3 Ajax
In the not-so-distant pa st, to retrieve new data from a web server, browsers ha d to
reload the whole web page. Not only was this slow due to the substantial amount
of data tha t ha d to be sent back and forth, but it was also uncomfor ta ble for a user
as the whole page disappeared and reappeared again even for updating the smallest
chunk of information from the server. Then , in 2005, the term Ajax was fashioned,
which o riginally stood for Asynchrono us JavaScript and XML. Ajax was first used
by Google in its magical Google Suggest, the other name for Go ogle’s auto-comp le te
functionality. It works so that when a visitor starts to enter letters in a search field, a
dropdown menu appears, sugg esting for the user possibly interesting terms. Although
the information is coming from the web server, the whole page stays put during the
operation. This is in fact the only—albeit extremely important—advantage of Ajax: it
permits a web page to be updated with information from the web server without ever
reloading the page. That way, web pages feel more responsive, greatly enh ancing the
user experience. How is tha t accomplished?
Ajax isn’t a technology in and of itself, but rather a n interaction o f more techn ologies
working together. You n eed Java Script, an XMLHttpRequ est object, and some server-
side scripting. The latter is, unfortunately, not covered in this book. Nevertheless, you
will ge t a basic idea of how Ajax works.
All the traffic between web browsers an d servers is specified by the Hypertext Trans-
fer Protocol (HTTP), which is usually not contro lled by scripts but happens as a con-
sequence of user actions su ch as following a link or typing a URL. That said, the
XMLHttpRequest obje ct defines an A PI that allows a programmer to con trol HTT P
requests through JavaScript code. Basically, JavaScript uses an XMLHttpRequest ob-
ject to send a request to a web server, which responds by sen ding some d ata back.
JavaScript then collec ts the rec eived data, again through the XMLHttpRequest object,
and does with th em whatever it likes.
Before you can use the just-mentioned HTTP API, you must cr eate an XMLHttpRe-
quest object:
var myRequest = new XMLHttpRequest();
Now you’re ready to ma ke an HTTP request, which is made by calling the open()
B.3. Ajax 283