Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Ajax Javascript Onload

  • 06-12-2007 12:21PM
    #1
    Registered Users, Registered Users 2 Posts: 3,484 ✭✭✭


    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, Registered Users 2 Posts: 3,484 ✭✭✭randombar


    Legend!


Advertisement