Click to See Complete Forum and Search --> : img swapping based on a variable


dburnham4739
11-21-2003, 11:44 AM
I receive an "image is undifined" error when I load this page. Can someone see what I am missing. This is only my second stab at Java, so be kind.

David









<img name="thermoImg">

<script language="javascript">
// creates image object to cache the images
imgOverTemp = new image;
imgNormalTemp = new image;

//pre-loads the images into the image objects
imgOverTemp.src = "../images/thermometerover.jpg";
imgNormalTemp.src = "../images/thermometernormal.jpg";

//Assigns the value of the iLon variable and displays the appropriate image

if (fahrenheit1round <= 85.0 )
{
// Normal Temp
document.thermoImg.src = imgNormalTemp.scr;
document.thermoImg.alt = "Normal Temp";

}
else
{
// Over Temp
document.thermoImg.src = imgOverTemp.scr;
document.thermoImg.alt = "Over Temp";
};

</script>
:)

gil davis
11-21-2003, 11:56 AM
Is this a typo?
document.thermoImg.src = imgNormalTemp.scr;

dburnham4739
11-21-2003, 12:11 PM
Thank you, that was a typo. However, the problem still exists.

David

gil davis
11-21-2003, 12:15 PM
Did you fix both of them, or just the first one?

dburnham4739
11-21-2003, 12:17 PM
Both,

David

gil davis
11-21-2003, 12:19 PM
You need to let the page load one of the images in order to have the image exist. However, if you are not going to be swapping the image except on load, that seems like a lot of overhead. It would be much easier to document.write the required image tag and be done with it. Is that what you are trying to do?

dburnham4739
11-21-2003, 12:24 PM
This page is viewing a teperature sensing device located on their network. I have the page reloading every 5 seconds. As long as the temperature is below a set point, I would like to display the "Normal" image, otherwise I would like to display the "Ovber Temp" image.

David

gil davis
11-21-2003, 12:33 PM
Then you don't need to do all that. Place this where the image needs to appear.

<script language="javascript">
if (fahrenheit1round <= 85.0)
{document.write("<img src='../images/thermometernormal.jpg'>);}
else
{document.write("<img src='../images/thermometerover.jpg'>);}
</script>
I assume that refreshing the page will change the var.

dburnham4739
11-21-2003, 01:03 PM
When I use this code I get
"Undeterminde String Constant"

I have matched character to character. The coding is definately more stright forward than what I was trying.

David

dburnham4739
11-21-2003, 01:42 PM
OK, I now have the page so it will load with out an error, however, I receive the dreaded RED X instead of the picture.

Both images have been loaded onto the server. I have even placed the both in various location as a catch all. Still no image.

David

gil davis
11-21-2003, 01:49 PM
Check spelling and capitalization.

dburnham4739
11-21-2003, 03:34 PM
OK, This is the code as I currently have it. I have change the img file names to eliminate spelling errors. I also have the script looking in different locations. I have placed both imgs in the root, images and forms folders.

I am still receiving the Rex X of death.

David


<script language="javascript">
if (fahrenheit1round <= 85.0)
{document.write('<img src="../forms/cold.jpg">' );}
else
{document.write('<img src="hot.jpg">' );}
</script>

dburnham4739
11-21-2003, 03:36 PM
As added information, I also have this code on the same page and it works fine.

David

<script language="javascript">

if (fahrenheit1round <= 85.0 )
{
freezer1State = "Freezer OK"
}
else
{
freezer1State = "OVER TEMP"
};

document.write(freezer1State);
</script>

gil davis
11-21-2003, 03:41 PM
I suggest that you create a test page with a regular image tag for each place you expect the image to be and see if the red x shows up.

You can also try clearing your cache.

dburnham4739
11-21-2003, 03:51 PM
Well, my brain must be traveling in the right direction as I just did this. The image I was looking for does appear using straight HTML.

I do appreciate all of your help. You have gotten me alot closer than I would have gotten on my own.

David

dburnham4739
11-21-2003, 04:01 PM
Thanks for all of your help. I am not exactly sure what happened, but I now have it working.

Thanks,
David