PDA

View Full Version : swap image on image onLoad



xld
01-18-2007, 08:44 PM
This might sound wierd, but how would I write a script that swaps images, once they are loaded, to another one? For example if I have a couple of images on a page I would like to change to the same image (for example black.gif) once the respective image is loaded, ie on img onLoad. Not on body onload.

Can this be done? Tried it a couple of times but cannot get it to work. Would be nice if you wouldn't have to name each image too..

Anyone know how to do this? Please?

Thanks!

jscheuer1
01-21-2007, 06:56 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function swap_slow(im_el, im_src){
im_el.swapped=true;
for (var i_tem = 0; i_tem < document.images.length; i_tem++)
if(document.images[i_tem]==im_el)
break;
setTimeout("document.images["+i_tem+"].src='"+im_src+"'", 3000);
}
</script>
</head>
<body>
<img src="white.gif" onload="if(!this.swapped){swap_slow(this, 'black.gif')};">
</body>
</html>

The red 3000 sets a delay of three seconds (3000 milliseconds). Adjust as desired.