Chapter 7. Ajax
Ajax is a cornerstone of high-performance JavaScript. It can be used to make a page load faster by delaying the download of large resources. It can prevent page loads altogether by allowing for data to be transferred between the client and the server asynchronously. It can even be used to fetch all of a page’s resources in one HTTP request. By choosing the correct transmission technique and the most efficient data format, you can significantly improve how your users interact with your site.
This chapter examines the fastest techniques for sending data to and receiving it from the server, as well as the most efficient formats for encoding data.
Data Transmission
Ajax, at its most basic level, is a way of communicating with a server without unloading the current page; data can be requested from the server or sent to it. There are several different ways of setting up this communication channel, each with its own advantages and restrictions. This section briefly examines the different approaches and discusses the performance implications of each.
Requesting Data
There are five general techniques for requesting data from a server:
XMLHttpRequest (XHR)
Dynamic script tag insertion
iframes
Comet
Multipart XHR
The three that are used in modern high-performance JavaScript are XHR, dynamic script tag insertion, and multipart XHR. Use of Comet and iframes (as data transport techniques) tends to be extremely situational, and won’t be covered here.
XMLHttpRequest
By far the most ...
Get High Performance JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.