Forum Moderators: open

Message Too Old, No Replies

onload problem with nested functions

onload works with one function, but not nested functions

         

swingandmiss

6:36 pm on Jun 6, 2005 (gmt 0)

10+ Year Member



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);
var finalRating = 0.0;
var count = 0;
for (var i in reviews) {
if (reviews[i][1] >= similarityLevel) {
finalRating += parseFloat(reviews[i][2]);
count++;
}
}
finalRating /= count;
writeLayer("overallRating", finalRating);
}
</script>

(I've left out a few functions 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?

(sorry about the formatting, I can't get spaces or tabs to work)

ChadSEO

8:14 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



You placed an alert() in the function writeLayer() to verify it is getting run? Did you verify it is getting a good value for the "txt"? If so, try placing alerts within each of the if statements, to see which branch it is taking. Also, I assume the browser is not generating any JS errors?