Accelerated DOM Scripting with Ajax, APIs, and Libraries by Dan Webb & Stuart Langridge & Aaron Gustafson & Jonathan Snook

Accelerated DOM Scripting with Ajax, APIs, and Libraries by Dan Webb & Stuart Langridge & Aaron Gustafson & Jonathan Snook

Author:Dan Webb & Stuart Langridge & Aaron Gustafson & Jonathan Snook [Dan Webb]
Language: eng
Format: epub
Tags: Design, Digital Media, JavaScript, Web Development
Publisher: Apress
Published: 2007-09-25T21:00:00+00:00


if(transport)

{

transport.open("GET", "http://example.com/test/", true);

transport.onreadystatechange = function(){ alert('I am back!'); };

transport.send();

} This is the basic structure of an Ajax request. The first part instantiates the XHR object, beginning with trying to instantiate a native version of the object. If a native implementation is not found, it attempts to instantiate the ActiveX objects for Internet Explorer (IE). (You'll learn more about why we check for both implementations later on.)

The second part takes the object and opens a connection with three parameters: the first specifies the method (GET or POST), the second is the URL you want to open, and the third determines whether the call should be synchronous or asynchronous.

If the third parameter is set to synchronous (false), the browser will wait for the call to return before users can do anything. This isn't ideal because users might think their browser has frozen and needs to be shut down. Setting it to true makes the call asynchronous, enabling the user to return to interact with the page while the call processes in the background.

The status of the XHR request makes a number of calls to the onreadystatechange event handler. From within the event handler, you can check the status of the call by checking the readyState property on the XHR object: transport. The readyState property will store a number between 0 and 4 at any given time (see Table 5-1).

Table 5-1 Possible readyState States Value State Description

0 Uninitialized The open method of the XHR object hasn't been called yet.

1 Loading The send method hasn't been called yet.

2 Loaded The send method has been called; response headers are available.

3 Interactive The response data is in the process of downloading, and is available via the responseText property of the XHR object.

4 Completed Everything is done, and the entire result is available in the responseText and, if available, the responseXML properties of the XHR object.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.