Click to See Complete Forum and Search --> : onload problem with nested functions


john2
06-06-2005, 03:23 PM
I'm having a problem with nested functions and onload and the following Javascript functions

<script language="javascript" type="text/javascript">
function writeLayer(layerID,txt){
if(document.getElementById){
document.getElementById(layerID).innerHTML=txt;
else if(document.all){
document.all[layerID].innerHTML=txt;
} else if(document.layers){
with(document.layers[layerID].document){
open();
write(txt);
close();
}
}
}
function updateRating(similarityLevel) {
http.open("GET", url, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
...
writeLayer("overallRating", finalRating);
}
</script>

(I've left out a few functions and some of the code in the fuctions to shorten the post.)

When I call writeLayer with a body onload the <div> get updated.

<body onload="javascript:writeLayer('overallRating','40')">

But when I call updateRating, which then calls writeLayer, from the onload the <div> never is updated.

<body onload="javascript:updateRating(40)">

I know the function is being run because I can add an alert which is displayed.

Anyone know why the <div> wouldn't be updated?

phpnovice
06-06-2005, 06:42 PM
Where does finalRating get assigned a value?

john2
06-06-2005, 10:46 PM
It gets assigned within the updateRating function. It's part of the code that is removed. It's there, as I can add an alert that will display it.

john2
06-06-2005, 11:13 PM
Hmm...tracking it down a little more.

updateRating calls the handleHttpResponse function. I know it's running as I can output the reviews array at the end of the function with an alert. Here's the code:

function handleHttpResponse() {
if (http.readyState == 4) {
// Split the delimited response into an array
reviews = http.responseText.split(";");
for (var i in reviews) {
reviews[i] = reviews[i].split(",");
}
return (reviews);
}
}

BUT, when I try to output the reviews array from the calling function updateRating (above) nothing happens. The alert box doesn't even show up. It's like the array doesn't exist (and the function ends abrubtly at that point. Nothing else in the fuction is run). But, when I call the updateRating function through an onclick after the initial page load, everything works fine.

john2
06-07-2005, 10:29 PM
Ok. I've restructured some of the functions now and things are almost working. Here's the new handleHttpResponse function.

function handleHttpResponse() {
if (http.readyState == 4) {
var count = 0;
var finalRating = 0;
// Split the comma delimited response into an array
reviews = http.responseText.split(";");
for (var i in reviews) {
reviews[i] = reviews[i].split(",");
}
for (var i in reviews) {
if (reviews[i][1] >= simLevel) {
finalRating += parseFloat(reviews[i][2]);
count++;
}
}
finalRating /= count;
writeLayer("overallRating",finalRating);
}
}
function updateRating(similarityLevel) {
simLevel = similarityLevel;
http.open("GET", url, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}

simLevel is a global because I couldn't get http.onreadystatechange = handleHttpResponse; to pass it successfully.

Now, to my next problem. The resulting <div> get's update with what appears to be a random NaN from handleHttpResponse. Is it ok to post a link here so that you can see what's going on?

phpnovice
06-07-2005, 11:20 PM
I'm afraid I won't be much help. I haven't used the XmlHttpRequest object before so I don't know anything about how it works. etc.