Javascript Loading Images

Discussion in 'Javascript' started by Robbie, Jun 4, 2004.

  1. Robbie

    Robbie Guest

    I have a simple script that changes the src and height and width of a
    <img>. But when I load the image it changes it size first, streching or
    shrinking the previous image, before changing the image. This is because
    the picture hasn't loaded yet. How can I have it so that it waits till
    the image is loaded before changing the height width and src? I have
    been playing around with img.complete and onload but it doesn't want to
    work...
     
    Robbie, Jun 4, 2004
    #1
    1. Advertising


  2. > How can I have it so that it waits till
    > the image is loaded before changing the height width and src?


    Best thing is to first preload the image before changing it.

    For example:

    var preload;
    var wait;
    var actualImage = document.images[0];

    function changeImage(src) {
    preload = new Image();
    preload.src = src;
    wait = window.setInterval("checkImage();", 500);
    }

    function checkImage() {
    if (preload.complete) {
    window.clearInterval(wait);
    actualImage.src = preload.src;
    // do your stuff
    }
    }
     
    Vincent van Beveren, Jun 4, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. j0vo
    Replies:
    2
    Views:
    395
  2. Vidar Petursson
    Replies:
    1
    Views:
    338
  3. MrMr[iCE]
    Replies:
    0
    Views:
    168
    MrMr[iCE]
    Aug 10, 2003
  4. stan k.
    Replies:
    3
    Views:
    151
  5. Replies:
    4
    Views:
    326
Loading...

Share This Page