Avatar of brad0525
brad0525
 asked on

Loading 2 ajax scripts body=onload

As you can see in the attached code I am trying to run 2 ajax scripts using body=onload... The second is the only one that loads, for some reason the first will never load if there are two onload request, but they both work perfectly when ran alone..
<script type="text/javascript">
function CDRInfo(str)
{
if (str=="")
  {
  document.getElementById("content").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("content").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","cdr_ajax.php?load_cdr_page="+str,true);
xmlhttp.send();
}
</script>

<script type="text/javascript">
function load(str)
{
if (str=="")
  {
  document.getElementById("ops").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("ops").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","operations.php?operation="+str,true);
xmlhttp.send();
}
</script>
</head>
<body onload="CDRInfo(1);load()">

Open in new window

AJAXJavaScript

Avatar of undefined
Last Comment
Göran Andersson

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Göran Andersson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Göran Andersson

On second thorught, as the variable is assigned in different if statements, you better declare it the first you do in the function:

  var xmlhttp;
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck