Connecting Tech Pros Worldwide Help | Site Map

Javascript Loading Images

  #1  
Old July 23rd, 2005, 12:23 PM
Robbie
Guest
 
Posts: n/a
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...
  #2  
Old July 23rd, 2005, 12:23 PM
Vincent van Beveren
Guest
 
Posts: n/a

re: Javascript Loading Images


[color=blue]
> How can I have it so that it waits till
> the image is loaded before changing the height width and src?[/color]

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
}
}



Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Questions about on-demand dynamic javascript loading Venkatesh answers 6 January 13th, 2007 05:05 AM
Loading images for use on client John answers 2 November 17th, 2005 06:51 PM
Place elements before loading images josvanr@xs4all.nl answers 9 July 23rd, 2005 07:14 PM
Javascript Menu Not Loading Images - (Now with linked Webpage) John Ortt answers 3 July 23rd, 2005 01:18 PM