AJAX - XMLHttpRequest Enhancements in Windows Internet Explorer 8
Windows Internet Explorer 8 enables finer control over Asynchronous JavaScript and XML (AJAX) requests. Specifically, developers now have the ability to specify a timeout for the XMLHttpRequest object, which, in combination with the increased number of concurrent connections enabled in Internet Explorer 8, can prevent delays in AJAX applications. An event handler for timeouts has also been added.
This topic contains the following sections.
- The XMLHttpRequest Object
- The timeout Property
- The ontimeout Event Handler
- Related topics
The XMLHttpRequest Object
The XMLHttpRequest object is a data transport object that is the core of AJAX. XMLHttpRequest was introduced in 2000, mainly to enable Microsoft Outlook Web Access to display e-mails without notification. Since then, AJAX applications have gained popularity for their ability to asynchronously exchange data with a server and then display that data without having to reload the Web page on which the data appears.
As AJAX has gained popularity, adoption of XMLHttpRequest has significantly exceeded original expectations. At the same time, its functional design has remained the same. In an effort to modernize XMLHttpRequest and increase reliability for today's AJAX-intensive applications, Internet Explorer 8 introduces new functionality to XMLHttpRequest.
The timeout Property
XMLHttpRequest became a native object in Windows Internet Explorer 7, and with Internet Explorer 8 it gains the timeout property. With the timeout property, Web developers can specify the length of time in milliseconds for the host to wait for a response before timing out the connection.
This provides several benefits. Because there are a limited number of connections that can exist between the host process and the server at one time, setting a timeout enables the host process to open a new request sooner, rather than waiting on a dead connection. This is especially relevant to users in narrowband scenarios—for instance, dial-up modem users—and can lead to better performance and less delay. For more information on connectivity and Internet Explorer 8, see Connectivity Enhancements in Internet Explorer 8.
Following is brief sample code that demonstrates how to use the timeout property. In this case, the timeout has been set to 10,000 milliseconds (10 seconds).
var xhr;
xhr = new XMLHttpRequest();
xhr.open("GET", "https://myurl.php", true);
xhr.timeout = 10000;
The ontimeout Event Handler
To instruct your application what to do in the event of a timeout, use the ontimeout event handler.
Following is brief sample code that demonstrates how to use the ontimeout event handler. In this case, when the ontimeout handler is raised, the timeoutRaised
method is called.
function timeoutRaised()
{
alert("timeout");
}
...
xmlHttpRequest.ontimeout = timeoutRaised;