Creating an XMLHttpRequest object

All modern web browsers provide an XMLHttpRequest class you can instantiate in your code, which you can use to issue asynchronous calls to obtain content over HTTP. You'll create one or more of these in your client-side JavaScript using the new operator.

How to do it...

You'll want to create an instance of this class early on in your JavaScript after the page loads, as shown in the following code:

function doAjax() {
var xmlhttp;
if (window.XMLHttpRequest)
  {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
}

How it works…

The preceding code tests the root-level JavaScript window object for the XMLHttpRequest class, and if the browser defines the class, creates an instance of the class ...

Get JavaScript JSON Cookbook 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.