Jump to content

Show animated GIF only when loaded


RLJ

Recommended Posts

Hi all,

 

I have an animated GIF banner on my website that is around 2MB. Currently, I use the following Javascript to preload the banner before the page is displayed:

 

<!-- preload images
banner = new Image();
banner.src = \"IMG/banner.gif\";
// End -->

 

However, for people with slower internet connections, I want to write some Javascript that will achieve the following:

 

- page loads up and displays with (small image size) loading.gif instead of banner.gif

- banner.gif starts downloading "in the background"

- when banner.gif has fully downloaded, loading.gif is replaced with banner.gif and banner.gif starts playing

 

This seems easy, but I'm new to Javascript so I could use some help! Can somebody show me how to do this?

 

Thanks!

 

P.S. if you're thinking why do I need Javascript to do this, it's because if I just include banner.gif as an HTML image (and without the pre-loading Javascript above), it starts playing (and jittering) before it's fully loaded. But perhaps this also depends on the browser.

 

Link to comment
Share on other sites

You're forcing the user to download >2MB worth of data? Not such a problem on unlimited plans, but for those with limits (and especially for those with limits and slower connections) they certainly won't want that! Why does a banner even take 2MB?

Link to comment
Share on other sites

I have tried the following:

<html> 
<head> 
<script type="text/javascript" language="javascript"> 
var banner = new Image();
var loading = new Image();
var bannerElement = document.getElementById("BANNER");
banner.src = "IMG/BANNER1-2.gif";
loading.src = "IMG/loading.gif";

banner.onload = function() {
    bannerElement.removeChild(bannerElement.lastChild);
    bannerElement.appendChild(banner);
};
bannerElement.removeChild(banner);
bannerElement.appendChild(loading);
</script> 
</head> 
    
<body> 
<div id="BANNER">
<img src="IMG/BANNER1-2.gif" alt="Banner" />
</div> 
</body> 

</html>

 

But it's not working, can someone see where I've gone wrong? Thanks.

 

Link to comment
Share on other sites

A thanks, but I had already tried that as well, it doesn't seem to be working. What I have is the following:

<html> 
<head> 
<script type="text/javascript" language="javascript"> 
function bannerload(){
var banner = new Image();
var loading = new Image();
var bannerElement = document.getElementById("BANNER");
banner.src = "IMG/BANNER1-2.gif";
loading.src = "IMG/loading.gif";

banner.onload = function() {
    bannerElement.removeChild(bannerElement.lastChild);
    bannerElement.appendChild(banner);
};
bannerElement.removeChild(banner);
bannerElement.appendChild(loading);
}
</script> 
</head> 
    
<body onload="bannerload()"> 
<div id="BANNER">
<img src="IMG/BANNER1-2.gif" alt="Banner" />
</div> 
</body> 

</html>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.