By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,606 Members | 1,796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,606 IT Pros & Developers. It's quick & easy.

multiple ajax calls simultaneously

P: 2
Hey,

In the following code the function showdivs() is called on body load, though only the second div is loaded. I've been searching on the web for similar problems and I know it has to do with the handler function, which is supposed to be run twice but now just cancels the first function as soon as the second one is called. Could anybody explain me how to solve this?
TIA,

Woods

Expand|Select|Wrap|Line Numbers
  1. var time_variable;
  2.  
  3. function getXMLObject()  //XML OBJECT
  4. {
  5.    var xobject = false;
  6.    try {
  7.      xobject = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
  8.    }
  9.    catch (e) {
  10.      try {
  11.        xobject = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
  12.      }
  13.      catch (e2) {
  14.        xobject = false   // No Browser accepts the XMLHTTP Object then false
  15.      }
  16.    }
  17.    if (!xobject && typeof XMLHttpRequest != 'undefined') {
  18.      xobject = new XMLHttpRequest();        //For Mozilla, Opera Browsers
  19.    }
  20.    return xobject;  // Mandatory Statement returning the ajax object created
  21. }
  22.  
  23. var xobject = new getXMLObject();    //xmlhttp holds the ajax object
  24.  
  25.  
  26. function handler() {
  27.     var getdate = new Date();
  28.  
  29.     if(xobject) {
  30.         xobject.open("POST",phpFile,true);
  31.         xobject.onreadystatechange  = function() {
  32.             if (xobject.readyState==4) {
  33.                 if(xobject.status == 200) {
  34.                     document.getElementById(divID).innerHTML=xobject.responseText;
  35.                 }
  36.                 else {
  37.                     alert('error');
  38.                 }
  39.             }
  40.         }
  41.         xobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  42.         xobject.send(postVars);
  43.     } 
  44. }
  45.  
  46.  
  47. function showdiv1() {
  48.       divID = "div1"; //define html div
  49.       phpFile = "showdiv1.php"; //define file to be loaded
  50.       postVars = "null"; //define post vars
  51.       new handler();
  52. }
  53.  
  54. function showdiv2() {
  55.       divID = "div2"; //define html div
  56.       phpFile = "showdiv2.php"; //define file to be loaded
  57.       postVars = "null"; //define post vars
  58.       new handler();
  59. }
  60.  
  61. function showdivs() {
  62. showdiv1();
  63. showdiv2();
  64. }
  65.  
Mar 9 '11 #1
Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
actually, both are loaded. it’s just that the second overwrites the first one.

PS. it’s null, not "null"
Mar 9 '11 #2

P: 2
Solved it - suppose nice to share?

Expand|Select|Wrap|Line Numbers
  1. var time_variable;
  2.  
  3. function getXMLObject()  //XML OBJECT
  4. {
  5.    var gobject = false;
  6.    try {
  7.      gobject = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
  8.    }
  9.    catch (e) {
  10.      try {
  11.        gobject = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
  12.      }
  13.      catch (e2) {
  14.        gobject = false   // No Browser accepts the XMLHTTP Object then false
  15.      }
  16.    }
  17.    if (!gobject && typeof XMLHttpRequest != 'undefined') {
  18.      gobject = new XMLHttpRequest();        //For Mozilla, Opera Browsers
  19.    }
  20.    return gobject;  // Mandatory Statement returning the ajax object created
  21. }
  22.  
  23. function handler(div) {
  24.  
  25.     var xobject = new getXMLObject();    //xmlhttp holds the ajax object
  26.     var randint = Math.random();
  27.     var getdate = new Date();
  28.  
  29.         xobject.open("POST",phpFile,true);
  30.         xobject.onreadystatechange  = function() {
  31.             if (xobject.readyState==4) {
  32.                 if(xobject.status == 200) {
  33.                     document.getElementById(div).innerHTML=xobject.responseText;
  34.                 }
  35.                 else {
  36.                     alert('error');
  37.                 }
  38.             }
  39.         }
  40.         xobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  41.         xobject.send(postVars);
  42.     } 
  43.  
  44.  
  45. function showdiv1() {
  46.       divID = "div1"; //define html div
  47.       phpFile = "showdiv1.php"; //define file to be loaded
  48.       postVars = "var1=somevalue"; //define post vars
  49.       handler(divID);
  50. }
  51.  
  52. function showdiv2() {
  53.       divID = "div2"; //define html div
  54.       phpFile = "showdiv2.php"; //define file to be loaded
  55.       postVars = "var2=somevalue"; //define post vars
  56.       handler(divID);
  57. }
  58.  
  59. function showdiv3() {
  60.       divID = "div3"; //define html div
  61.       phpFile = "showdiv3.php"; //define file to be loaded
  62.       postVars = "verzend=somevalue"; //define post vars
  63.       handler(divID);
  64. }
  65.  
  66. function showdivs() {
  67. showdiv1();
  68. showdiv2();
  69. showdiv3();
  70. }
  71.  
Mar 11 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.