View Full Version : swap image on image onLoad

01-18-2007, 09: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?


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

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