Chapter 5 — How Gmail Works 61
Finding XMLHttpRequest within the Gmail code
Don’t take the presence of XMLHttpRequestwithin Gmail on trust. You can see
this in action in Gmail’s own code. Go back to the DOM inspector and open the
second frameset — the one with all of the JavaScript in it. Copy the entire script
into a text editor and save it, as you’re going to refer to it a lot in this section.
Once you’ve done that, search for the string xmlhttp. You’ll find the function in
Listing 5-2.
Listing 5-2:Gmail’s XMLHttpRequest Function
function zd(){var R=null;if(da){var
vN=lJ?”Microsoft.XMLHTTP”:”Msxml2.XMLHTTP”;try{R=new
ActiveXObject(vN)}catch(f){C(f);alert(“You need to enable active
scripting and activeX controls.”)}}else{R=new
XMLHttpRequest();if(!R){;alert(“XMLHttpRequest is not supported on
this browser.”)}}return R}
As with all of the Gmail JavaScript, this is compressed and slightly confusing.
Reformatted, it looks like Listing 5-3.
Listing 5-3:Gmail’s XMLHttpRequest Function, Tidied
function zd(){
var R=null;
if(da){
var vN=lJ?”Microsoft.XMLHTTP”:”Msxml2.XMLHTTP”;
try{R=new ActiveXObject(vN)}
catch(f){
C(f);alert(“You need to enable active scripting and
activeX controls.”)}
}else{
R=new XMLHttpRequest();
if(!R){
;alert(“XMLHttpRequest is not supported on this
browser.”)}
}
return R}
This listing does exactly the same thing you did earlier: tries out the Microsoft
Active X controls, then tries the more standard XMLHttpRequestand then, if all
fails, bails with an error message. For future reference, and remember this because
you’ll need it later, the XMLHttpRequestobject in the Gmail code is called R.