[ka-Map-users] xhr.js mod
Brent Pedersen
bpederse at gmail.com
Fri Oct 7 14:13:29 EDT 2005
hi, i tweaked xhr.js a bit below. interface to rest of kaMap is unchanged.
might be cvs worthy.
'garbage collection' for aXmlHttp via splicing.
closure returned from XmlHttpGen means the ActiveX/xmlttprequest test is
performed only on page load.
may be __slight__ performance gains from the closure.
// XMLhttpRequest stuff
var aXmlHttp = new Array();
function xmlResult()
{
for(var i=0;i<aXmlHttp.length;i++)
{
if(aXmlHttp[i][0] &&
aXmlHttp[i][0].readyState==4&&aXmlHttp[i][0].responseText)
{
var f = aXmlHttp[i][2];
var o = aXmlHttp[i][1];
var s = aXmlHttp[i][0].responseText;
aXmlHttp.splice(i--,1);
f.apply(o,[s]);
}
}
// return aXmlHttp.length;
}
// u -> url
// o -> object (can be null) to invoke function on
// f -> callback function
// p -> optional argument to specify POST
function call(u,o,f)
{
var method = "GET";
var dat;
if (arguments.length==4){
method = "POST";
tmp = u.split(/\?/);
u = tmp[0];
dat = tmp[1];
}
var idx = aXmlHttp.length;
aXmlHttp[idx]=new Array(2);
aXmlHttp[idx][0] = getXMLHTTP();
aXmlHttp[idx][1] = o;
aXmlHttp[idx][2] = f;
if(aXmlHttp[idx])
{
aXmlHttp[idx][0].open(method,u,true);
if(method == "POST"){
aXmlHttp[idx][0].setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
aXmlHttp[idx][0].send(dat);
}
aXmlHttp[idx][0].onreadystatechange=xmlResult;
if(method =="GET"){ aXmlHttp[idx][0].send(null);}
}
}
getXMLHTTP = xmlHttpGen();
function xmlHttpGen()
{
if(typeof XMLHttpRequest != "undefined")
{
return function(){return new XMLHttpRequest();}
}
var msv= ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
for(j=0;j<=msv.length;j++){
try
{
A = new ActiveXObject(msv[j]);
if(A){
return function(){return new ActiveXObject(msv[j]);}
}
}
catch(e) { }
}
return false;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.maptools.org/pipermail/ka-map-users/attachments/20051007/c42872a0/attachment.html
More information about the ka-Map-users
mailing list