Connecting Tech Pros Worldwide Help | Site Map

Javascript Loading Images

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 11:23 AM
Robbie
Guest
 
Posts: n/a
Default Javascript Loading Images

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, 11:23 AM
Vincent van Beveren
Guest
 
Posts: n/a
Default 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
}
}



 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.