|Read more about this book|
(For more resources on this subject, see here.)
Ajax and the XMLHttpRequest object
Human speech: An overlaid function
Human speech is an overlaid function. What is meant by this is reflected in the answer to a question: “What part of the human body has the basic job of speech?” The tongue, for one answer, is used in speech, but it also tastes food and helps us swallow. The lungs and diaphragm, for another answer, perform the essential task of breathing. The brain cannot be overlooked, but it also does a great many other jobs. All of these parts of the body do something more essential than speech and, for that matter, all of these can be found among animals that cannot talk. Speech is something that is overlaid over organs that are there in the first place because of something other than speech.
Ajax: Another overlaid function
What exactly is this overlaid function?
Ajax is a way of using client-side technologies to talk with a server and perform partial page updates. Updates may be to all or part of the page, or simply to data handled behind the scenes. It is an alternative to the older paradigm of having a whole page replaced by a new page loaded when someone clicks on a link or submits a form. Partial page updates, in Ajax, are associated with Web 2.0, while whole page updates are associated with Web 1.0; it is important to note that “Web 2.0” and “Ajax” are not interchangeable. Web 2.0 includes more decentralized control and contributions besides Ajax, and for some objectives it may make perfect sense to develop an e-commerce site that uses Ajax but does not open the door to the same kind of community contributions as Web 2.0.
Some of the key features common in Web 2.0 include:
An emphasis on user-centered design
Enabling community participation to update the website
Enabling information sharing as core to what this communication allows
The concept of “partial page updates” may not sound very big, but part of its significance may be seen in an unintended effect. The original expectation of partial page updates was that it would enable web applications that were more responsive. The expectation was that if submitting a form would only change a small area of a page, using Ajax to just load the change would be faster than reloading the entire page for every minor change. That much was true, but once programmers began exploring, what they used Ajax for was not simply minor page updates, but making client-side applications that took on challenges more like those one would expect a desktop program to do, and the more interesting Ajax applications usually became slower. Again, this was not because you could not fetch part of the page and update it faster, but because programmers were trying to do things on the client side that simply were not possible under the older way of doing things, and were pushing the envelope on the concept of a web application and what web applications can do.
The technologies Ajax is overlaid on
Now let us look at some of the technologies where Ajax may be said to be overlaid.
- The Wikipedia article says it was designed to resemble Java but be easier for non-programmers, a decision reminiscent of SQL and COBOL.
- The Java programmer who finds the C-family idiom of for(i = 0; i < 100; ++i) available will be astonished to find that the functions are clobbering each other’s assignments to i until they are explicitly declared local to the function by declaring the variables with var. There is more pain where that came from.
The following two functions will not perform the naively expected mathematical calculation correctly; the assignments to i and the result will clobber each other:
result = 0;
for(i = 0; i < 100; ++i)
result += inner(i);
result = 0;
for(i = 0; i < limit; ++i)
result += i;
There are differences and some incompatibilities between different versions of XMLHttpRequest, and efforts are underway to advance “level-2-compliant” XMLHttpRequest implementations, featuring everything that is expected of an XMLHttpRequest object today and providing further functionality in addition, somewhat in the spirit of level 2 or level 3 CSS compliance. We will not be looking at level 2 efforts, but we will look at the baseline of what is expected as standard in most XMLHttpRequest objects.
The basic way that an XMLHttpRequest object is used is that the object is created or reused (the preferred practice usually being to reuse rather than create and discard a large number), a callback event handler is specified, the connection is opened, the data is sent, and then when the network operation completes, the callback handler retrieves the response from XMLHttpRequest and takes an appropriate action.
A bare-bones XMLHttpRequest object can be expected to have the following methods and properties.