PDA

View Full Version : variable wrong on 1st load


Tails
02-06-2003, 11:38 PM
I have a big problem. I'm trying to write a script that detects the
width of the browser window and make images in a table wrap. My formula for t below is get the amount of times an image of 105
width can be displayed in a certain width of the width of the window without scrolling. (the images are actually 100px but I am taking margins and cellspacing into effect as an estimate). And the images are supposed to wrap upon loading. But When the page loads, v returns as nothing (the first value assigned). I originally omitted the FORM part thinking that the values assigned to v in d() would pass on. But they didn't. So when I made the form to get the values passed on and reloaded, v once again returned nothing. But when reloaded, v gets the proper value that was assigned in d(). Why? Strangely, it will keep
showing the proper value from then on when you refresh (but I need it to be right the first time). But it will start at nothing again if you make a change to the page and refresh. Odd way of detecting a change in a page other than lastModified. This just proves how browsers are so arrogant that they know what they are doing when you tell them to refresh. In this case, it only reinterprets the data if it has changed and gets different results from the first loading and it's "reloading". If they can't get offline pages (the most predictable) right, how they gotten as far as functioning online is now a mystery to me. Ponder that. I've tested this on Win98 IE 5.0. I have noticed that on Win2k and XP, refreshing a page is never enough. Making a change to a script
and refreshing will not tell you the correct line where an error is.
You have to close IE and open the file again for the correct area. What a pain!



<BODY bgcolor="#00ace0" onLoad="d()">

<FORM name="A">
<INPUT type="hidden" name="B">
</FORM>

<SCRIPT>
P=Array("14","15","21","22","26","28","29","37","38","39","57","59")
w=document.body.clientWidth
t=Math.floor((w-260)/105)
v=""
function d()
{
for (i=0; i<t; i++)
{
v+='<TD><IMG src="tn'+P[i]+'.jpg"></TD>'
}
document.A.B.value=v
}


alert(document.A.B.value)
document.write("<TABLE border><TR>"+v+"</TR></TABLE>")
</SCRIPT>

</BODY>

Algorithm
02-06-2003, 11:55 PM
The only reason this works on refresh is because IE doesn't automatically clear its form values when a page is reloaded.

What you're doing is printing v BEFORE you call the function that assigns anything to it, so the only way anything will be printed is if the page is refreshed.

To fix this, you need to put the document.write() code inside the function declaration.

Tails
02-07-2003, 08:16 PM
But the first thing that should happen is the onLoad to assign the value to v. Could it be that v+= returns nothing because the onLoad took higher priority than assigning v="" and caused problems? I may have tried already, but maybe v should be first assigned in the function. Wait...maybe the function assigned the value and THEN it was victim of being intercepted by the reassigning of v=""? Do you think so?

Roy Sinclair
02-07-2003, 09:25 PM
Originally posted by Tails
But the first thing that should happen is the onLoad to assign the value to v. Could it be that v+= returns nothing because the onLoad took higher priority than assigning v="" and caused problems? I may have tried already, but maybe v should be first assigned in the function. Wait...maybe the function assigned the value and THEN it was victim of being intercepted by the reassigning of v=""? Do you think so?

No the last thing that happens is the function d getting called. That call is happening after the document.write because you call that function when the onload event occurs, all the other code inside your <script></script> tags runs before the function d is called because that code is executed as the browser processes it.

Tails
02-08-2003, 06:26 PM
Really, I didn't know that. So then the onLoad in the body tag will happen last even after script tags that go after the body tag? I'm sure it's not that important this far, now that I know the main order of priorities. Thanks!

Roy Sinclair
02-10-2003, 04:12 PM
Correct, the idea of the onload tag is to schedule an action to occur after the whole rest of the page has finished loading (images and everything). Inline code will run while the page is loading which places it as occuring before the onload event.

beetle
02-10-2003, 04:17 PM
Originally posted by Tails
So then the onLoad in the body tag will happen last even after script tags that go after the body tag?Script tags don't belong outside of <body> or <head>. Internet Explorer will still run them :rolleyes:, but any version of netscape won't.

So, whether or not the onload runs before/after such tags is moot. That's just something you shouldn't do.

Tails
02-10-2003, 09:04 PM
Not that I care much about Netscape, but if a SCRIPT tag is in a DIV tag which is of course in the BODY tag, that is still allowed, correct?

beetle
02-10-2003, 09:08 PM
Yes.