Java Reference
In-Depth Information
if (tempRequest.readyState == 4)
{
if (tempRequest.status == 200)
{
fpCallback(tempRequest.responseText);
}
else
{
alert(“An error occurred trying to contact the server.”);
}
}
}
this.request.onreadystatechange = request_readystatechange;
}
The new lines of code may again look strange. The fi rst new line creates the tempRequest variable.
This variable is a pointer to the current object's request property, and it's used within the request_
readystatechange() function. This is a technique to get around scoping issues. Ideally, you would
use this.request inside the request_readystatechange() function. However, the this keyword
points to the request_readystatechange() function instead of to the XMLHttpRequest object, which
would cause the code to not function properly. So when you see tempRequest , think this.request .
Inside the request_readystatechange() function, you see the following line:
fpCallback(tempRequest.responseText);
This line calls the callback function specifi ed by the constructor's fpCallback parameter, and you pass
the responseText property to this function. This will allow the callback function to use the information
received from the server.
Creating the Methods
There are two methods in this reference type: one is used inside the constructor, and the other enables
you to send the request to the server.
Cross-Browser XMLHttpRequest Creation … Again
The fi rst method is createXmlHttpRequest() . The inner workings of cross-browser object creation
were covered earlier in the chapter, so let's just see the method defi nition.
HttpRequest.prototype.createXmlHttpRequest = function ()
{
if (window.XMLHttpRequest)
{
var oHttp = new XMLHttpRequest();
return oHttp;
}
else if (window.ActiveXObject)
{
var versions =
[
Search WWH ::




Custom Search