IE7's Inanimate GIF
Posted by Ryan Baxter Wed, 11 Jun 2008 12:06:00 GMT
Animated GIFs are most often used as activity indicators in modern AJAX (asynchronous JavaScript and XML) enabled websites. I decided to use a GIF (Graphic Interchange Format) from ajaxload.info on an application that I’ve been developing at work. The application performs server-side processing on files uploaded by employees. Processing time varies depending on the size of the file. Larger files take longer.
I wanted the animated GIF to appear when the file upload button was clicked so I placed the image within a DIV tag and hid it by setting a blank CSS display property. OnClick of the upload button, a JavaScript function toggled the display value to “block”, making the DIV appear. This worked as expected in Firefox and IE6, but not in IE7. The DIV appeared in IE7, but it’s GIF wasn’t moving.
A little googling turned up a helpful comment on Rick Strahl’s blog.
I’ve run into a problem with animated gifs inside of a hidden area of a Web page that is hidden with style.display=’none’. When the area is made visible again, in Internet Explorer this causes the image to not be displayed an animated GIF whatever I try. [sic]
Apparently IE7 doesn’t like to animate hidden GIFs. User submitted comments on Rick Strahl’s blog provided many solutions, but only a couple worked in my situation. The first uses the JavaScript setTimeout method to populate the image’s SRC attribute 200 ms after the function call. The second sets the DIV’s innerHTML to a string containing an IMG tag with the animated GIF.
<script language='javascript'>
function ShowLoading(elementId)
{
document.getElementById(elementId).style.display = "block";
setTimeout('document.images["loadingImage"].src = "../images/loading.gif"', 200);
}
</script>Neither solution is ideal, but setting the element’s innerHTML felt a little bit cleaner to me.
<script language='javascript'>
function ShowLoading(elementId)
{
var element = document.getElementById(elementId);
element.innerHTML = "<img src='../images/loading.gif'>";
element.style.display = "block";
}
</script>*IE7 may have animations disabled. Go to Tools > Internet Options > Advanced > Multimedia. Checking “Play animations in webpages” may affect how IE7 renders your animated GIFs.
- Posted in Code Snippets, Expect the Unexpected
- Meta no trackbacks, 2 comments, permalink, rss, atom
Trackbacks
Use the following link to trackback from your own site:
http://crunchlife.com/articles/trackback/68
Comments
3 months later:
4 months later:

