Results 1 to 2 of 2

Thread: swap image on image onLoad

  1. #1
    Join Date
    Jan 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking swap image on image onLoad

    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!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,127
    Thanks
    44
    Thanked 3,228 Times in 3,189 Posts
    Blog Entries
    12

    Default

    Code:
    <!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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •