Click to See Complete Forum and Search --> : Why doesn't onmouseover work in a function?
jammer20002
09-17-2003, 11:09 AM
Why doesn't a onmouseover function work in a function?
I've got the following code (snippet):
<!-- Begin
var image0 = new Image(); image0.src = "blank.jpg";
var image1 = new Image(); image1.src = "map.jpg";
// End -->
function swapTheImages()
{
image0.src='1.jpg'; image1.src='2.jpg'; image2.src='2.jpg';
}
When I do an onmouseover and call the function, it doesn't work. Bust if I put the code in directly - it works!
Why is this? :(
I'm pulling my hair out.
Please attached text file for full code.
Thanks.
Jam
gil davis
09-17-2003, 03:35 PM
You are getting the image variables confused with the image objects. The image variable only helps to preload the images that will be swapped. They do not appear anywhere. So if you change the image variable, you don't make any visible changes. You have to change the object to see a change.
<script>
var image0 = new Image();
image0.src = 2.jpg; // this preloads the image to save time
function swap() {
document.images.image0.src = image0.src;
}
</script>
...
<image name="image0" src="1.jpg">
BTW, it will not do any good to preload "1.jpg" since it is loaded in the actual HTML anyway.
jammer20002
09-17-2003, 04:41 PM
Thank for the reply.
Unfortunatley, I get an error saying "document,images.imageo is null or not an object".
Any other suggestions?
With regards to what you said about the image variables and objects does make sense. But the thing is I originally copied the code from javascriptsource. And that's how someone who posted a script was doing the image swap?
It certanly works when you DON'T do it as a function call.
Any other suggestions?
I'm desperate. :(
jammer20002
09-17-2003, 06:38 PM
I got it to work at last! :)
You gave me the code:
document.images.image0.src = image0.src;
I had a look at some other postings of code and tried the folloiwng instead:
document.image0.src = image0.src;
This seems to have done the trick.
BUT... should have what you gave worked?
gil davis
09-18-2003, 08:59 AM
document.images is an array, and usually you can access that array using document.images.image0. Most browsers also allow document.image0 or document.images["image0"].