Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some help with Java Script

Status
Not open for further replies.

jphillips

Programmer
Nov 20, 2001
24
0
0
US
Long shot here but I thought I might ask.

I have some code. 2 functions. I can not quite fiqure out what it is doing. It appears to be returning some type of array but not sure how to handle results[]

/// -----------------------------------------------
function handleHttpResponse() {
if (http.readyState == 4) {
result = http.responseText.split("||");
image_GetCode();
if (!result[0]){
alert ("Opps there was a problem");

return;
}
if (result[2]){
var visit = confirm(result[2]);
if (visit) test_LoadURL(result[1]);
return;
}
if (result[1])
alert(result[1])
else {
alert(result[0]);
document.getElementById("val_1").value = "";
document.getElementById("val_2").value = "";
document.getElementById("val_3").value = "";
}
}
}



function image_GetCode(){
var currentTime = new Date();
var newtime = currentTime.getTime();
document.getElementById("Image-CodeImg").setAttribute("src", " + newtime);
}
 
The first function is testing to see if the server has finished sending back the results of a request sent to it then it does a split on the response.Text into the array response then calls the image_GetCode function.
It then tests if any values were stored in the array and if not sends out an error and exits.
Then if there is a value in result[2] it prompts you for an answer and if the answer is true it runs another function then exits.
If you get this far, then if there is a value in result[1] it pops up that value and if no value there it pops up the value of result[0] and resets the values in the form elements val_1,2 and 3.

The image_GetCode function gets the current time then sets the src attribute of an image on the page to the specified URL with the time appended to the end of the URL.


Stamp out, eliminate and abolish redundancy!
 
Thanks
Little beefer than I need.
How could I change the code for an array to do a popup that said Success the code worked or Nope it didn't work

Thanks
 
It depends on what you are testing.
What is the nature of your array, what are you looking for in the array to cause a popup?

Stamp out, eliminate and abolish redundancy!
 
I am trying to return some values from a firefox tool bar with 3 inputs. Fill in the tool bar, pass the vars to a php page, insert it into the database and return either succes or failure popup. The popup would also eval the three parms and say oops you forgot var1 or var2 or var3.

I have the tool bar built and is working (inserting data into a mysql database) but I not sure how to handle the return so I can tell the end user if it was successful or not, with the above code. I can eval everything on the php side and return

Thanks for the help.

 
Well it looks like the code above does indeed test the returned info.

I assume you pulled this code from an app that works similarly to what you are trying to do and are trying to figure out how it works?

I have no way of knowing how the HTTP request is being made but essentially after it is made the handleHttpResponse function should be called.
The function starts by testing to see if the http.readyState is 4 which would indicate the request has completed and then the rest of the function will execute.
If when you call the function the state is not 4 the function does not go any further so I assume you would need to either wait long enough to know it would have finished before calling the function or you would have to setup a loop to repeatedly call the function until either it is able to complete or it reaches a maximum number of tries that you set.

Right now the function is not returning a value. If the readstate is 4 it begins executing the if statements which may or may not cause a return but the returns have no value.
I assume a return without a value would be false but do not know that.
But if the readstate never reaches equals 4 the function does not send a response back. So you should probably setup a loop with a maximum number of tries so it will not lock up the browser in an infinite loop if it does not get a good response.

This is all theory for me as I have never done anything like this before so all I can do is make suggestions and you will have to figure out how they fit your app.

My suggestion is to test with a simplified version of the function.
Code:
function HttpResponseTest() {
  if (http.readyState == 4) {
    result = http.responseText.split("||");
    if (!result[0]){
      alert("No results returned");
    }
    else {
      for (var x=0; x<result.length; x++) {
        alert("Result " + x + " = " + result[x]);
      }
    }
    return true;
  }
  return false;
}

This function if the readystate equals 4 will test to see if there is a result value returned. If there is no value it pops up an error, otherwise it will popup each value returned then it will return true to the calling function letting it know that it completed so it can break out of any loop it uses. If readystate does not equal 4 it returns false to the calling function.

So at the end of whatever function you use to do the HTTPRequest setup a loop to call the test function above that executes long enough for you to know if it worked or not.


Stamp out, eliminate and abolish redundancy!
 
Yea that is what I been trying to fiqure out. I think I am going to throw out all this and just return yes is pass if the insert worked and false with what is not right I will eval it on the php side of things, since I don't know a whole bunch on the java script side.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top