Skip to content
Donner's Daily Dose of Drama
Donner's Daily Dose of Drama
  • The Good
    • Blogging
    • Consumer Protection
    • Environment
    • Ethics
    • Geek’s Home
    • Lisa Lanett
    • Medfield
    • Music
    • Parenting and Technology
    • Travel
    • wow
  • The Bad
    • Business
    • Ebay
    • Investment
    • Job search
    • Personal Finance
    • Politics
  • The Ugly
    • Information Technology
      • Business Intelligence
      • Content Management
      • Free Software
      • I18N and L10N
      • Java
      • Open Source
      • Mobile Devices
      • Open Source Business Intelligence
      • OSBI
      • SDA
      • Security
      • Smartphone
      • Software Best Practices
      • Software Engineering
      • SQL Server
      • Streaming Media
      • Web
    • Austria
    • Fiction
    • Hardware
    • iPod
    • Miscellaneous
    • Uncategorized
    • Video
    • Weekend Warrior
Donner's Daily Dose of Drama

The HTML <IMG> tag and “Onload” event bugs in IE

Christian Donner, January 18, 2009March 3, 2009

The problem: a blog post is supposed to show a temperature chart. The chart is served by a remote server, running SQL Reporting Services. The table with the temperature data contains 100,000s of rows and the server that hosts the SQL database and Reporting Services is an older Athlon box. Serving that image can take a while.

Because it was not apparent to the site user that an image was being loaded, I wanted some kind of visual indicator – an animated gif that would be replaced with the chart once it had finished loading. After googling around a bit, I settled for the following approach:

 <img id="chartimg" onload="swap('chartimg','http://charturl');" 
     src="animation.gif" />
 <script type="text/javascript">
 function swap(imgID, imgSrc)
 {
   theImg=document.getElementById(imgID);
   if (theImg.src != imgSrc) // prevent event recursion
       pic1.onload = imgSrc;
 }
 </script>

This code already contains several iterations of attempts to fix the basic issue that IE shows. One is that you get into a recursive loop because the onload event fires when the src attribute is changed. Since the event handler is assigning a new image source, it would cause the event to fire again etc, resulting in a stack overflow error.

The other problem is that IE seems to process the tag parameters in the wrong order, firing the onload event prior to the src attribute being assigned. This is what most other people seem to have run into, based on other posts that I found.

So, the above code already takes care of both these issues – by testing the src attribute for the new URL, causing the onload event to fire only twice (once for the initial load of the animated GIF, the second time when the URL of the chart image is assigned).

This works in principle. The problem that I was still left with was that IE did not animate the GIF while the chart was loading in the background, no matter what I tried. For instance, using a temporary image variable did not fix the problem. The issue is that the thread handling the tag is locked up while any image loads. Since the animation was a key part of the requirement, I did not want to settle for a static image.
I tried to come up with a creative solution, such as waiting for the load into a Javascript variable to finish prior to assigning the SRC attribute a new value, but the Javascript function returns immediately and we never know when that is the case. Also, there is no such thing as an Onload event for a variable containing an image.
The only solution: The 2nd image needs to have its own tag, so that the firing of the Onload event does not interfere with the GIF animation.

The final solution below does exactly that – one IMG tag shows the animated GIF and a second hidden one is used to load the chart image. The hidden image has the Onload event handler. It fires when the chart has fully loaded and is available, and the event handler replaces the animated GIF with the chart image.

<img id="chartimg" src="progress_circle.gif" />
<img id="hiddenImg" onload="swap('chartimg','hiddenImg');"
    src="http://somesite/chart_url.aspx" style="display: none;"/>
<script type="text/javascript">
function swap(img1ID, img2ID)
{
  theImg1 = document.getElementById(img1ID);
  theImg2 = document.getElementById(img2ID);
  if (theImg1.src != theImg2.src)  //prevents recursive events
    theImg1.src = theImg2.src;
}
</script>

To view the result, visit http://medfieldblogs.net/temperature-in-medfield.

I realize this code does not validate, but it works, and that is all I needed.

Related Posts:

  • OpenVPN
  • The Great Cat Litter Poop Off
  • The Voip.ms SMS Integration for Home Assistant
  • Computer Build 2025
  • A box of SUTAB

Software Engineering Web

Post navigation

Previous post
Next post

Comments (2)

  1. Gauri says:
    September 16, 2010 at 2:16 am

    hi
    below is my code… it works fine in IE for some images and in firefox(Mozilla/5.0) it gives height and width both as zero.

    function getImgSize(imgsrc)
    {
    var newImg = new Image();
    newImg.onLoad = function() {
    // always called
    alert(‘image loaded’);
    };
    newImg.src = imgsrc;
    var height = newImg.height;
    var width = newImg.width;
    var filename = imgsrc.replace(/^.*\\/, ”)
    alert(“‘” + filename + “‘ is ” + width + ” by ” + height + ” pixels in size.”);
    alert (‘The image size is ‘+width+’*’+height);
    }

  2. Dirk Dittmar says:
    March 16, 2011 at 9:04 am

    thanks for the effort… the hint with the animated gif was very helpful…

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About
  • Awards
    • TechnoLawyer
  • Contact Christian Donner
  • Project Portfolio
  • Publications
  • Speaking Engagements

Recent Comments

  • Christian Donner on Sealing a leaky cast-iron fireplace chimney damper
  • Eric on Sealing a leaky cast-iron fireplace chimney damper
  • Christian Donner on Contact Christian Donner
  • Max on Contact Christian Donner
  • Christian Donner on Contact Christian Donner

Tags

AHCI Amazon Android ASP.Net AT&T Droid Drupal email Error failure featured firmware Garmin Godaddy Google honda Internet Explorer 8 iPhone Lenovo Lisa Lanett Modules NAS Nexus One Paypal Performance Privacy QNAP raid RS-407 sauna Security spam SQL SR3600 Synology T-Mobile T430s transmission tylö Verizon Virus VMWare Windows 7 windows 8.1 Windows Mobile
  • About
  • Awards
    • TechnoLawyer
  • Contact Christian Donner
  • Project Portfolio
  • Publications
  • Speaking Engagements
©2025 Donner's Daily Dose of Drama | WordPress Theme by SuperbThemes