GetXmlHttpObject() for multiple objects

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jul 2007
Posts: 30
Reputation: Mylena is an unknown quantity at this point 
Solved Threads: 1
Mylena Mylena is offline Offline
Light Poster

GetXmlHttpObject() for multiple objects

 
0
  #1
Aug 27th, 2008
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

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. var xmlHttp1
  3. var xmlHttp2
  4. var xmlHttp3
  5.  
  6. function showCustomer(str)
  7. {
  8. xmlHttp1=GetXmlHttpObject();
  9. xmlHttp2=GetXmlHttpObject();
  10. xmlHttp3=GetXmlHttpObject();
  11.  
  12. if (xmlHttp==null)
  13. {
  14. alert ("Your browser does not support AJAX!");
  15. return;
  16. }
  17. var url1="getcustomer1.asp";
  18. url1=url1+"?q="+str;
  19. url1=url1+"&sid="+Math.random();
  20. var url2="getcustomer2.asp";
  21. url2=url2+"?q="+str;
  22. url2=url+"&sid="+Math.random();
  23. var url3="getcustomer3.asp";
  24. url3=url3+"?q="+str;
  25. url3=url3+"&sid="+Math.random();
  26. xmlHttp1.onreadystatechange=stateChanged;
  27. xmlHttp1.open("GET",url,true);
  28. xmlHttp1.send(null);
  29. xmlHttp2.onreadystatechange=stateChanged;
  30. xmlHttp2.open("GET",url,true);
  31. xmlHttp2.send(null);
  32. xmlHttp3.onreadystatechange=stateChanged;
  33. xmlHttp3.open("GET",url,true);
  34. xmlHttp3.send(null);
  35. }
  36.  
  37. function stateChanged()
  38. {
  39. if (xmlHttp1.readyState==4 || xmlHttp2.readyState==4 || xmlHttp3.readyState==4)
  40. {
  41. document.getElementById("txtHint1").innerHTML=xmlHttp1.responseText;
  42. document.getElementById("txtHint2").innerHTML=xmlHttp2.responseText;
  43. document.getElementById("txtHint3").innerHTML=xmlHttp3.responseText;
  44. }
  45. }
  46.  
  47. function GetXmlHttpObject()
  48. {
  49. var xmlHttp1=null;
  50. var xmlHttp2=null;
  51. var xmlHttp3=null;
  52. try
  53. {
  54. // Firefox, Opera 8.0+, Safari
  55. xmlHttp1=new XMLHttpRequest();
  56. xmlHttp2=new XMLHttpRequest();
  57. xmlHttp3=new XMLHttpRequest();
  58. }
  59. catch (e)
  60. {
  61. // Internet Explorer
  62. try
  63. {
  64. xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");
  65. xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
  66. xmlHttp3=new ActiveXObject("Msxml2.XMLHTTP");
  67. }
  68. catch (e)
  69. {
  70. xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
  71. xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
  72. xmlHttp3=new ActiveXObject("Microsoft.XMLHTTP");
  73. }
  74. }
  75. return xmlHttp;
  76. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: GetXmlHttpObject() for multiple objects

 
1
  #2
Sep 1st, 2008
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
Attached Files
File Type: js agent.js (8.4 KB, 31 views)
Google is the answer to all of your questions -- the trick is knowing what question to ask in your specific predicament.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC