Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Ajax Javascript Onload

Options
  • 06-12-2007 12:21pm
    #1
    Registered Users Posts: 3,401 ✭✭✭


    Trying to run two ajax scripts at onload to populate two divs!

    when i run them in the following order:

    showPubList(0,0);
    showCalendar(0,0);

    the calendar div is popluated correctly and not the pub div


    when i run them in the following order:

    showCalendar(0,0);
    showPubList(0,0);

    the pub div is popluated correctly and the calendar div is populated with the pub div

    Doing my head in!
    <script type="text/javascript">
    
    var xmlHttp;
    
    function showCalendar(countyid,month)
    { 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
     {
     alert ("Browser does not support HTTP Request")
     return
     }
    var url="ajax.php?calendar=1"
    url=url+"&countyid="+countyid
    url=url+"&month="+month
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChangedCalendar
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }
    
    
    
    function showPubList(countyid,letter)
    { 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
     {
     alert ("Browser does not support HTTP Request")
     return
     }
    var url="ajax.php?publist=1"
    url=url+"&countyid="+countyid
    url=url+"&letter="+letter
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChangedPubList
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }
    
    
    function stateChangedPubList() 
    { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     { 
     document.getElementById("txtPubList").innerHTML=xmlHttp.responseText;
     } 
    }
    
    function stateChangedCalendar() 
    { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     { 
     document.getElementById("txtCalendar").innerHTML=xmlHttp.responseText;
     } 
    }
    
    
    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
     {
     // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
     }
    catch (e)
     {
     //Internet Explorer
     try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
     catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
     }
    return xmlHttp;
    }
    </script>
    


Comments

  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    Its because "xmlHttp.responseText" is not get unique values - its always going to contain the value of whatever function is being run last

    for example, running it this way

    showCalendar(0,0);
    showPubList(0,0);

    xmlHttp.responseText - will always have the values from showPubList(0,0)

    likewise if you run it the other way

    showPubList(0,0);
    showCalendar(0,0);

    xmlHttp.responseText - will always have the values from showCalendar(0,0);


  • Registered Users Posts: 3,401 ✭✭✭randombar


    Legend!


Advertisement