XMLHttpRequest) {
transport = new XMLHttpRequest();
}else{
try{ transport = new ActiveXObject("MSXML2.XMLHTTP.6.0"); }catch(e){}
try{ transport = new ActiveXObject("MSXML2.XMLHTTP"); }catch(e){}
}
if(!transport) return;
this.transport = transport;
}
Ajax.prototype.send = function(url, options)
{
if(!this.transport) return;
var transport = this.transport;
var aborted = false;
var _options = {
method:"GET",
timeout:5,
onerror:function(){},
onsuccess:function(){}
};
// override options
for(var key in options)
{
_options[key] = options[key];
}
function checkForTimeout()
{
if(transport.readyState != 4)
{
aborted = true;
transport.abort();
}
}
setTimeout(checkForTimeout, _options.timeout * 1000);
CHAPTER 5 n AJAX AND DATA EXCHANGE 120
function onreadystateCallback()
{
if(transport.readyState == 4)
{
if( !aborted && transport.status >= 200 && transport.status < 300 )
{
_options.onsuccess(transport);
}else{
_options.onerror(transport);
}
}
}
transport.open(_options.method, url, true);
transport.onreadystatechange = onreadystateCallback;
transport.send('');
}
A bunch of new stuff has been added here, so let??™s go through things one chunk at a time:
var aborted = false;
var _options = {
method:"GET",
timeout:5,
onerror:function(){},
onsuccess:function(){}
};
The aborted variable is a flag you??™ll use to determine later whether you had to abort the
call manually.
Pages:
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188