Re: JavaScript Tutorial Question

by "The Web Center" <admin(at)webctr.com>

 Date:  Fri, 24 Sep 1999 05:35:32 -0400
 To:  "Denise" <denise(at)nf.sympatico.ca>
 Cc:  <hwg-basics(at)hwg.org>
 References:  denise
  todo: View Thread, Original
This is a normal programming technique to swap variable values.

A variable is the name by which something is referred to (image1 is a
variable for the file thau.gif).

A value is the "something" that is being referred to (thau.gif)

if image1 is already assigned a variable, but you want to trade it another
variable's value (the value of image 2, for instance), you can't just do
this:

image 1 = image2;

image2 = image1;

what would happen?   image1 = image2 would make them both point to sky.gif,
because it assigns the value of image2 to image1, but without changing
image2 at all.  So, the next line (image2 = image1) just sends the value
"sky.gif" back to image2!

The way around this is to make a quick temporary variable (in this case
called "temp") to hold onto image1's value for a sec, so we can use it when
ready:

// assign initial values...
image1 ="thau.gif";
image2 ="sky.gif";

// store value of "thau.gif" for a sec...
temp = image1;

// make image1 point at sky.gif
image1=image2;

// make image2 point at than.gif
image2=temp


Hope this helps...writing it left me confused...:)

Darrell

The Web Center
Web Site Solutions
admin(at)webctr.com
www.webctr.com

----- Original Message -----
From: Denise <denise(at)nf.sympatico.ca>
To: <hwg-basics(at)mail.hwg.org>
Sent: Thursday, September 23, 1999 10:02 PM
Subject: JavaScript Tutorial Question


> I'm just starting to learn JavaScript, and I'm using the Web Monkey
tutorial
> as my starting point. I got through the tutorial pretty well until it came
> to the image swapping 'homework'. I couldn't get it to work after over an
> hour of trying, so I took a peek at the code (cheating, I know) Even with
> the code in front of me, I still am not quite sure whats going on in this
> script.
>
> What exactly does this line do?
>  onMouseOver="temp=image1; image1=image2; image2=image3; image3=temp;
> document.the_image.src=image1;"
> onClick="document.brand_image.src=image1;">
>
>
> Here's the full script.....
>
> <script language="javascript">
> <!-- hide it
>
> var temp = "";
> var image1 = 'thau.gif';
> var image2 = 'sky.gif';
> var image3 = 'monkey.gif';
> -->
> </script>
>
> </head>
> <body>
> <div align="center">
> <img src= "monkey.gif" name="brand_image">
> </div>
> <div align="right">
> <a href="#"
> onMouseOver="temp=image1; image1=image2; image2=image3; image3=temp;
> document.the_image.src=image1;"
> onClick="document.brand_image.src=image1;">
> <img src="monkey.gif" name="the_image" border=0></a>
>
>
> Any help with this, on or off list, would be really appreciated.
>
>
> Thanks,
>
> Denise
> denise(at)nf.sympatico.ca
>

HTML: hwg-basics mailing list archives, maintained by Webmasters @ IWA