10AJAX Technology (Asynchrony)

This chapter deals with synchronous and asynchronous processing.

Programming in the web ecosystem, that is to say, in the context the Internet, means that the speed of communication and the volume of exchanged data impact the processes. Response time can vary, and an absence of response may block a process. JavaScript is run in “single thread”, hence one single blocking operation may block the whole application: we need another mechanism for creating a request and handling the return (“callback”) into the event loop previously described (section 8.5).

10.1. Architecture for client-server data exchange

Dynamically adding elements in a web page traces back to 1996, with the <iframe>. In 1998, Microsoft introduced a more general solution with the ActiveX controls, then in the form of the object XMLHttpRequest in Internet Explorer. In 2005, that object was standardized by ECMA and was popularized under the name Asynchronous Javascript And XML (AJAX).

The client-side script program can post a HTTP request and then continue its activity without being blocked. When the mechanism is able to tell that an answer (or an error) is ready, the callback function can be run, on that response, in a similar manner to that of the processing of DOM events.

Beyond the ability to modify only part of the web page, avoiding a complete reload (including many unchanged elements), AJAX also allows access to the data files, under a variety of formats (primarily XML), served ...

Get JavaScript and Open Data now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.