XMLHttpRequest In Gears

The XMLHttpRequest object is implemented in Gears, which turn it into a special Ajax framework whose aim is mainly to use web applications offline.
Features of the XHR object are so extendend and is is able to exchange Blobs or texts between the browser on a side and on the other side the server or the local computer.
A Blob is an object added to JavaScript by Gears that holds a binary block of data.

To use XHR in Gears:

  1. Install the plug-in on your browser.
  2. Include the gears_init.js script (see resources belows to get it).
  3. Add the following JavaScript code:
<script type="text/javascript">
  function xhr()
     var storage = document.getElementById("storage");
     var request = google.gears.factory.create('beta.httprequest');
     request.open('GET', 'xhr-demo.txt');
     request.onreadystatechange = function() 
          if (request.readyState == 4) 
               storage.innerHTML = request.responseText;

The difference with the classic Ajax code is the use of google.gears.factory.create('beta.httprequest') to create the object, which alone replaces constructors for different browsers (See XHR for details).

The above example loads the xhr-demo.txt file that contains the string "Hello World!"
And the contents of the file is displayed in the <div> whose ID is "storage".

Demonstration : XMLHttpRequest in gears

Using the XMLHttpRequest object in Gears. The object is created by the Google's API that in turn will use the right constructor for the current browser.

If the Gears plug-in is installed on the browser, the message "Hello World!" must be displayed below in big letters: