1

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>
Community
  • 1
  • 1
MountainMan
  • 753
  • 2
  • 10
  • 20
  • have you tried `$(document).ready()`? – Pankit Kapadia Dec 17 '12 at 05:23
  • 3
    What are `Biggie` and `MONDUX`? _They_ must learn to cooperate. This _will_ call both. – John Dvorak Dec 17 '12 at 05:23
  • 1
    How sure are you that *both* functions aren't causing an error of some kind? In that case it doesn't matter which order the functions are called in, processing will cease before the other function is called. – Brian Lacy Dec 17 '12 at 05:25
  • AWAL = Absent Without Authorized Leave? – John Dvorak Dec 17 '12 at 05:25
  • @Pandit Nope, not familiar with that. More ideas to study, thanks. – MountainMan Dec 17 '12 at 05:25
  • @MountainMan what Pankit is suggesting is the use of the [jQuery library](http://jquery.com). It's quite powerful. – John Dvorak Dec 17 '12 at 05:26
  • @ Biggie and MONDUX are 2wo different functions... er. AJAX thingyies and they both work perfectly, independently. Just having trouble getting them both accessible at once. They use different .php scripts for their calls, cannot imagine any conflicts there. Hmm.. – MountainMan Dec 17 '12 at 05:28
  • @Brian If they both asynchronous, would it matter the order they load? – MountainMan Dec 17 '12 at 05:29
  • 1
    @MountainMan If the first one throws an exception, the other one will not run. Can you show us their code? Also please look if there's anything in the javascript console. – John Dvorak Dec 17 '12 at 05:31
  • @Jan Brilliant: I use that tool for inspecting elements and css etc., didn't realize it was a javascript debugging tool also.. thanks, and on to something: – MountainMan Dec 17 '12 at 05:37
  • Uncaught ReferenceError: serverResponse is not defined 000%208%20KISS%20JavaScript%20MyASS,%20PHP.php:55 – MountainMan Dec 17 '12 at 05:38
  • @MountainMan now you just need to find out what `serverResponse` should have been. Can you show the code around? – John Dvorak Dec 17 '12 at 05:42
  • @Jan. totally. it has no value, it is entirely learning material. I am a little gun~shy about putting up *walls of code* some folks freak if something is more than 15 lines. How do they... nevermind. I fixed the serverResponse thing, the alerts work now, annoying as they are, but still only the one or the other function, working. – MountainMan Dec 17 '12 at 05:49
  • gotta run, cafe closing... I am on this until I figure it out. Thanks. – MountainMan Dec 17 '12 at 05:57
  • tried localizing the variables ? e.g `var xmlhttp=...` else everthing be global in one function you might be creating it and in the other overwriting it . So try creating local variables first , and let me know if it works – sakhunzai Dec 17 '12 at 06:13

2 Answers2

1

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.

user4815162342
  • 2,058
  • 3
  • 18
  • 23
  • 1
    @sakhunzai: Yes, I saw you ninja'd me on that after I posted. Sorry. – user4815162342 Dec 17 '12 at 06:27
  • @user4815162342 magia! :) var xmlhttp; right after, below, the line : function X(){ ...just, wow. Now to use it, see if I can learn WHY it works. Thanks... I am trying to learn just enough to put a JavaScript UI for going to whatever image of 159, the user wishes to got to, on the website listed on my profile. Now, having a way to get multiple variables on the main php script will help. – MountainMan Dec 17 '12 at 06:48
  • I'll leave that figuring out WHY as an exercise for you :) – user4815162342 Dec 17 '12 at 07:11
  • @user4815162342 getting warmer..[link](http://www.howtocreate.co.uk/tutorials/javascript/functions") Good stuff. Section, "Scopes have memory" – MountainMan Dec 20 '12 at 00:13
0

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.

Milind Thakkar
  • 980
  • 2
  • 13
  • 20