Change images with javascript
Change images on mouse over with javascript. Define the images and preload them.
It can sometimes be a nice effect to change images when the mouse is moved over.
For example if you have a file archive function and have directories, then the directory images can “open” when the mouse is moved over.
Move over these images and see how the directory seem to open
This script does two nice things. First it “pre loads images”. If you don’t do this it will take a little time when the mouse is moved over an icon the first time, because the web browser needs to download the image. With pre loading we trick the browser to load the image before it is displayed.
With javascript we create new Image()-objects and assign four icons to them. Two for open/closed for the first image and two for the second. (Add more if you need more than two icons)
Then there are two functions “over” and “out” that take one parameter that tell the function what image to handle. 1 or 2 at the moment.. Those functions “simply” change the image-src into open/closed mode.
If you look at the links there are two new things. onMouseOver and onMouseOut. They are automaticly called as events when the mouse moves over or moves away from the link/image. Replace # with the file you want to be called when the link is clicked.
<script language=”JavaScript”>
<!–
// Pre load images.
if (document.images) {
img1_open = new Image();
img1_closed = new Image();
img2_open = new Image();
img2_closed = new Image();
img1_open.src = “open1.gif”;
img1_closed.src = “closed1.gif”;
img2_open.src = “open2.gif”;
img2_closed.src = “closed2.gif”;
}
function over(i)
{
if (document.images)
eval(‘document.img’+i+’.src=img’+i+’_open.src’);
}
function out(k)
{
if (document.images)
eval(‘document.img’+k+’.src=img’+k+’_closed.src’);
}
//–>
</script>
<a href=”#” onMouseOver=”over(1);” onMouseOut=”out(1);”>
<img name=img1 border=0 alt=”Changing image…” src=”closed1.gif”></a>
<a href=”#” onMouseOver=”over(2);” onMouseOut=”out(2);”>
<img name=img2 border=0 alt=”Changing image…” src=”closed2.gif”></a>



June 18th, 2009 at 5:48 am
Dear friend,
I am sure that your code is correct, but I typed it in to one of my pages, and after two days I still cannot get it to work. I am confused by the concept that we have two pics (Say “a” & “b”. With mouseover we need to change from “a” to “b”, so why are there 4 mentioned? I swapped the pic filenames around several times, but to no avail.
I don’t expect you have time to check other people’s coding, but the page in question in on my site’s ‘leslie.htm’, or from my navigation buttons ‘My Background’. The normal image is ‘les1a.jpg’, and the mouseover image should be ‘les1b.jpg’. I am to the point of giving up, but it would have been a nice effect if I could get it working?
Regards … Leslie
July 8th, 2009 at 2:43 am
You can only use two images if you like. The second two are for showing that you can switch between to different if you have for example one directory icon and one delete icon.
September 23rd, 2009 at 6:05 am
16 Errors, 3 warning(s) while validating
September 23rd, 2009 at 6:11 am
The script is a simple example, more to show how to get started with javascript and image changing. So there might be errors yes
February 23rd, 2010 at 7:59 am
i need this