Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 for us to use in the making of asynchronous requests.

See also

If you're working with a very old version of Internet Explorer, you may need to use a Microsoft.XMLHTTP ActiveX object. In which case, the test for window.XMLHttpRequest will...