Solved

Loading 2 ajax scripts body=onload

Posted on 2010-09-07
2
638 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:brad0525
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 33619781
You haven't declared the xmlhttp variable as local, so both functions use the same global variable. That means that the second function overwrites the XMLHttpRequest object created by the first function, so when the callback set by the first function tries to check the status of the response, it instead checks the status of the other request.

Change both occurances of this:

  xmlhttp=new XMLHttpRequest();

into:

  var xmlhttp=new XMLHttpRequest();

This will make the variables local. This also means that a closure will be created for each callback, which contains the local variable so that the code in the callback function can use it.
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 33619816
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;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question