Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I test for a preloaded image being fully downloaded? 2

Status
Not open for further replies.

Ineedjava

Technical User
Apr 14, 2001
21
US
I am preloading a large animated gif file. When fully downloaded, the animated gif will replace (swap out) a small static gif file.

So how do I test for when the image is fully downloaded? Using "onload=" in the BODY statement doesn't work because the web page has loaded (triggering the onload condition) but the image is still preloading.

Below is the real simple code I've written so far. As you can see, I have the swap event being triggered by a timer because I don't know when the animated gif has been fully downloaded. THanks!

------- Swap Image Code ----------------------

<html>
<script>

// Preload the large animated image

var image1 = new Image();
image1.src = &quot;LargeAnimated.gif&quot;;


// Wait 19 seconds to be sure large image is downloaded then execute switchImage function

function timeLapse()
{
setTimeout(&quot;switchImage('logo')&quot;,19000);
}


// Routine to switch large image with small static image.

function switchImage(imageTagName)
{
document.images[imageTagName].src = image1.src;
}

</script>

<body onload=&quot;timeLapse()&quot;>

This is a test of preloading of an image. A large animated logo is being preloaded while a small static logo is displayed.

<img src=&quot;SmallStatic.gif&quot; name=&quot;logo&quot;>

</body>
</html>
 
try onload of img tag:

var image1 = new Image();
image1.src = &quot;LargeAnimated.gif&quot;;
image1.onload=function(){
//make your swap
}


Victor
 
I've used the following line in the past to wait until an image has loaded.

var image1 = new Image();
image1.src = &quot;LargeAnimated.gif&quot;;
while (!image1.complete){}

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top