Chapter 32. Using XMLHttpRequest Objects

In Lesson 31 you learned that there are a variety of ways to incorporate Ajax into a web application. The older technique of using hidden frames can still be found in many modern web applications, but the most popular way to add Ajax functionality to an application is by using the XMLHttpRequest (XHR) object. Despite its name, you can retrieve more than XML with XHR. In fact, it's more commonly used to retrieve plain text.

The XHR object originated as a component, called XmlHttp, in Microsoft's MSXML library. The component was first released with Internet Explorer (IE) 5. XmlHttp allowed developers to make HTTP requests, using JavaScript or VBScript, to retrieve XML data. Microsoft updated the MSXML library with every major version of IE, and each new version included an improved XmlHttp component.

As the popularity of Microsoft's XmlHttp grew, the developers behind Mozilla built their own version (calling it XMLHttpRequest) for Firefox. Even though it had a different name, the Mozilla developers copied the majority of the properties and methods of Microsoft's XmlHttp, making cross-browser use easier. Soon after, the developers of Opera and Safari copied Mozilla's XHR object, making transparent HTTP requests through pure JavaScript possible in all modern browsers.

CREATING XHR OBJECTS

There are essentially two types of browsers when it comes to XHR support. The first supports the native XHR object—meaning that XHR is built into the browser. The ...

Get JavaScript® 24-Hour Trainer 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.