Hello everyone,

I need to create several xmlHttp objects, but I only want to have one GetXmlHttpObject() function, so that I don't have to repeat for each object. Below code is definitely wrong because... how do I return the needed object from the GetXmlHttpObject() function?

Really appreciate,

M

var xmlHttp1
var xmlHttp2
var xmlHttp3

function showCustomer(str)
{ 
xmlHttp1=GetXmlHttpObject();
xmlHttp2=GetXmlHttpObject();
xmlHttp3=GetXmlHttpObject();

if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url1="getcustomer1.asp";
url1=url1+"?q="+str;
url1=url1+"&sid="+Math.random();
var url2="getcustomer2.asp";
url2=url2+"?q="+str;
url2=url+"&sid="+Math.random();
var url3="getcustomer3.asp";
url3=url3+"?q="+str;
url3=url3+"&sid="+Math.random();
xmlHttp1.onreadystatechange=stateChanged;
xmlHttp1.open("GET",url,true);
xmlHttp1.send(null);
xmlHttp2.onreadystatechange=stateChanged;
xmlHttp2.open("GET",url,true);
xmlHttp2.send(null);
xmlHttp3.onreadystatechange=stateChanged;
xmlHttp3.open("GET",url,true);
xmlHttp3.send(null);
}

function stateChanged() 
{ 
if (xmlHttp1.readyState==4 || xmlHttp2.readyState==4 || xmlHttp3.readyState==4)
{ 
document.getElementById("txtHint1").innerHTML=xmlHttp1.responseText;
document.getElementById("txtHint2").innerHTML=xmlHttp2.responseText;
document.getElementById("txtHint3").innerHTML=xmlHttp3.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp1=null;
var xmlHttp2=null;
var xmlHttp3=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp1=new XMLHttpRequest();
  xmlHttp2=new XMLHttpRequest();
  xmlHttp3=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttp3=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
    xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
    xmlHttp3=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
Member Avatar for langsor

In concept ...

By using a javascript-class approach, rather than a procedural approach you can instantiate multiple object instances of the class ... or you could simply reuse the same object-instance by passing different request-strings and even target (server-side-script) actions to the object.

I'm uploading my latest ajax class for you to play with ... it seems to be working but there may still be some bugs in it that I haven't found yet -- no promises.

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.