Forum Moderators: open

Message Too Old, No Replies

I am having trouble understanding when JavaScript is automatically

invoked inside the head tags.

         

MarcMiller

4:51 am on Oct 2, 2005 (gmt 0)

10+ Year Member



I am having trouble understanding when JavaScript is automatically invoked inside the head tags. In the code below I created a little experimental page to see how to change images with JavaScript. The form buttons change the image in accordance with the button pressed as I expect. The function revoked by the event handler onload in the head tag also changes the image from what it would have been as expected. However the line <!--document.images['chpeople'].src =people[3]; --> which is line 34 in my code when it is line numbered, and not commented it out as I have done here, does nothing. And on Firefox is JavaScript console I get the following warning." Warning: reference to undefined property document.images.chpeople Source File: file:///c:/htmlplay2/pro1LeadImage/imagesByButtos_funtion.htm Line: 34" which does not appear when the same code is on the on click event handler in the form button or the function invoked by the onload event handler in the body tag. I was pretty sure I have read JavaScript between the head tags, in an HTML document, is automatically invoked. But it is not happening here and is causing the warning in the JavaScript console. So could someone please explain it to me I would appreciate it very much.
Truly Marc

<!--<HTML>
<HEAD>
<META NAME="Generator" CONTENT="Stone's WebWriter 3.5">
<TITLE>imagesByButtos</TITLE>

<SCRIPT TYPE="text/javascript">

var pr1= new Image();
pr1.scr="images1.jpeg"

var pr2= new Image();
pr2.scr="images2.jpeg"

var pr3= new Image();
pr3.scr="images3.jpeg"

var pr4= new Image();
pr4.scr="images4.jpeg"

var pr5= new Image();
pr5.scr="images5.jpeg"

var pr6= new Image();
pr6.scr="images6.jpeg"

var people=new Array( pr1.scr, pr2.scr, pr3.scr, pr4.scr, pr5.scr, pr6.scr);
function change()
{
document.images['chpeople'].src =people[1];
}
document.images['chpeople'].src =people[3];
</SCRIPT>
</HEAD>

<BODY onload="change()">
<IMG NAME = "chpeople" SRC="images1.jpeg" BORDER="0" ALIGN="bottom" ALT="changing people"><BR>

<form>
<INPUT TYPE="button" VALUE="image1" name="ch1" onclick="document.images['chpeople'].src =people[0]">
<INPUT TYPE="button" VALUE="image2" name="ch2" onclick="document.images['chpeople'].src =people[1]">
<INPUT TYPE="button" VALUE="image3" name="ch3" onclick="document.images['chpeople'].src =people[2]">
<INPUT TYPE="button" VALUE="image4" name="ch4" onclick="document.images['chpeople'].src =people[3]">
<INPUT TYPE="button" VALUE="image5" name="ch5" onclick="document.images['chpeople'].src =people[4]">
<INPUT TYPE="button" VALUE="image6" name="ch6" onclick="document.images['chpeople'].src =people[5]">
</form>

</BODY>
</HTML>-->

Bernard Marx

8:33 am on Oct 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



document.images['chpeople'].src =people[3]; 

This line is not inside a function, and thus executed immediately (as soon as it is read). At that time, the image element( document.images['chpeople'] ) doesn't yet exist, since the HTML for it has not been parsed.

This is why the onload event is so often used for things like this.

(note: The collection, document.images does itself exist at the time, but is empty)

rocknbil

11:13 pm on Oct 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A little off-topic, and perhaps this is something I don't know but worthy of mention in reference to the preload.

var pr1= new Image();

Creates an image object.

pr1.scr="images1.jpeg"

Assigns the image as the source file of the object. Usually.

What is scr? If you mean that to preload an image, it should be pr1.src.