MountainMan
MountainMan

Reputation: 785

How Can 2 onload() JavaScript Scripts be Loaded?

With this function:

function start() {
MONDUX();
Biggie();
}  

function MONDUX executes, and the AJAX call returns good data and is displayed correctly. However, Biggie() is a.w.a.l.

The result of this :

function start() {
Biggie();
MONDUX();
}  

is the opposite. Biggie() works as expected, MONUX() fails.

This doesn't do any good, down in the body:

<script type="text/JavaScript">
window.onload= start();
</script>  

and, this dodge is not helpful:

<body onload="start()">  

and that was tried like so also

Detest cargo~cult programming and running out of ideas here. Suggestions?

These resources were all related // near hits // no cigar. Loading javascript in body onload with 2 functions JS and Body(Window) Onload event JavaScript: How is "function onload() {}" different from "onload = function() {}"? That one was fascinating but way deep waters for me... How to onload two javascript files? meh... good, but...

?? :/~

<script type="text/javascript" >

 function MONDUX(){
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("WhatThexBobby").innerHTML=xmlhttp.responseText;
  }
 }
xmlhttp.open("POST","000 8 KISS 22solo PHP.php?figure1=5&figure2=33", true);
xmlhttp.send();

 alert(WhatThexBobby);

}
 </script>
 <script type="text/javascript" >

function Biggie(){

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("FreakinEh").innerHTML=xmlhttp.responseText;
 }
 }
 xmlhttp.open("POST","000 8 KISS solo PHP.php?figure1=5&figure2=10", true);
xmlhttp.send();

alert(FreakinEh);
 }


 </script>

Upvotes: 1

Views: 336

Answers (2)

user4815162342
user4815162342

Reputation: 2087

You're assigning the request to the global variable xmlhttp, and then reassigning that variable to another request before the first one has returned. I don't know if that is causing your problem, but it's definitely going to cause a problem. It's also very bad JavaScript practice.

Simple fix is to put the line 'var xmlhttp;' at the beginning of both functions.

Edit: Just in case you didn't know this: xmlhttprequest is asynchronous. You call 'send', and your remaining statements in the script and document continue to run while the request is being sent to the server. Only after the server returns do the various callback methods (onreadystatechange, and the like) get called, and this is long after your alerts were shown.

Upvotes: 1

Milind Thakkar
Milind Thakkar

Reputation: 980

Considering one of them is throwing some error, would it not be good idea to put them in Try Catch ? Something like,

function start() {
  try
  {
    MONDUX();
  }
  catch(err)
  {
   // handle error
  }
  try
  {
    Biggie();
  }
  catch(err)
  {
    //Handle error
  }
  finally
  {
   // cleanup
  }
}  

This will ensure both runs even if one of them mis-fires.

Upvotes: 0

Related Questions