IE9 image load event bug

I just found a problem with IE9 in how it handles the load event for img tags. With IE8 and earlier if I wanted to perform some JS function after an image loaded I could do something like this:

img = new Image();
img.src = ‘some.jpeg’;
if(img.complete){
somefunc();
} else {
img.attachEvent( “onload”, somefunc );
}

but with IE9 the event will never fire regardless of whether the image file is in the browser cache or not. To fix this you just need to set the img src after attaching the event or reset the img src after attaching the event like so:

img = new Image();
img.src = ‘some.jpeg’;
if(img.complete){
somefunc();
} else {
img.attachEvent( “onload”, somefunc );
img.src = img.src;
}

Hopefully this will help someone else out running into this same problem and encourage the IE team to fix it.

No related posts.

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

6 Comments

  1. Bill
    Posted June 7, 2011 at 6:52 am | Permalink

    Hi I was wondering if there was a way I could contact you to ask you a few questions about the computer repair field. I’ve been running a computer repair business for a little while now and am confused about a couple of non-tech factors of the business.
    Thanks

  2. M
    Posted June 17, 2011 at 2:10 am | Permalink

    Is it really a bug? Or a side-effect that we had been previously coding against?

    Loosely trying to make sense of the whole thing…
    If you set the ‘src’ of the image before you bind to the onload event. Wouldn’t it be possible that the onload event had fired after you set the ‘src’ but before you attached to the event?

  3. Pascal
    Posted June 23, 2011 at 6:57 am | Permalink

    Thank you so much! Just saved me couple of hours debugging IE9.

  4. Joe
    Posted July 11, 2011 at 8:43 pm | Permalink

    If i were to start my own PC Repair business, what tools and equipment should i start out with besides a toolbox i received as part of my Online PC Repair courses..Is there anything else? Should i have more than one test computer?..Diagnostic PCI Devices etc.?..Extra parts?…..Thanx…Your site is very helpul :)

  5. admin
    Posted July 12, 2011 at 7:48 am | Permalink

    You really don’t need much beyond a few simple hand tools most of which are never needed. Expensive diagnostic equipment is nice but not necessarily because almost all components can be tested by simply swapping them into another system. Extra parts are good to keep around, spare hard drives and power supplies are particularly useful but most customers are understanding if parts have to be ordered.

  6. Posted April 26, 2012 at 12:17 pm | Permalink

    I would like to note that this problem is not fixed to date, even in the IE10 which is shipped with Windows 8 preview.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>