views:

1295

answers:

3

I'm using JavaScript with the jQuery library to manipulate image thumbnails contained in a unordered list. When the image is loaded it does one thing, when an error occurs it does something else. I'm using jQuery load() and error() methods as events. After these events I check the image DOM element for the .complete to make sure the image wasn't already loaded before jQuery could register the events.

It works correctly except when an error occurs before jQuery can register the events. The only solution I can think of is to use the img onerror attribute to store a "flag" somewhere globally (or on the node it's self) that says it failed so jQuery can check that "store/node" when checking .complete.

Anyone have a better solution?

Edit: Bolded main points and added extra detail below: I'm checking if an image is complete (aka loaded) AFTER I add a load and error event on the image. That way, if the image was loaded before the events were registered, I will still know. If the image isn't loaded after the events then the events will take care of it when it does. The problem with this is, I can easily check if an image is loaded already, but I can't tell if an error occurred instead.

+5  A: 

Check the complete and naturalWidth properties, in that order.

http://www.sajithmr.me/javascript-check-an-image-is-loaded-or-not/

function IsImageOk(img) {
    // During the onload event, IE correctly identifies any images that
    // weren’t downloaded as not complete. Others should too. Gecko-based
    // browsers act like NS4 in that they report this incorrectly.
    if (!img.complete) {
        return false;
    }

    // However, they do have two very useful properties: naturalWidth and
    // naturalHeight. These give the true size of the image. If it failed
    // to load, either of these should be zero.

    if (typeof img.naturalWidth != “undefined” && img.naturalWidth == 0) {
        return false;
    }

    // No other way of checking: assume it’s ok.
    return true;
}
SLaks
Interesting, I was looking at that post earlier and I didn't realize that they were doing ! complete for IE as naturalWidth is not defined in IE. Going to check it now.
William
I think I should have explained better in my main question. I'm calling this after I add the events for load,error so that if it already had an error I will know. This method returns false even if a method is still "loading" and it has 0 errors. I need something to check if it has had an error, if it's still loading / complete then it should return true. Thanks for the answer though
William
You should edit your question.
SLaks
Updated, let me know if you want anymore details. Thanks for the help
William
+2  A: 

Another option is to trigger the onload and/or onerror events by setting the image's src to empty string then back to the original src. Here's an example of what I mean:

$(img).load(function() { console.log("image loaded correctly"); }
      .error(function() { console.log("error loading image"); }

var imgSrc = img.src;
img.src = "";
img.src = imgSrc; // Triggers onload and onerror events.

Hope this helps!

Xavi
Hmm that seems to work, and after a few tests it doesn't seem to hurt the loading of the image either. Thanks!
William
Doesn't seem to be working as well as I hoped. In Google Chrome it seems to be having issues when the image is already in cache. It doesn't trigger the load() method. :(
William
Ugg, you're right. Chrome is definitely the most annoying browser to develop for. On the bright, I think I may have found a work around: set the image source to "" then back to the original source. I'll update my answer.
Xavi
Looks like we were both thinking the same thing. It seems to work so far. I'll write back if I find another bug but so far it's working good. :)
William
Doesn't play well with IE6. It likes to cause an error event when changing the src. Also, in IE6 when you change the src it creates a new HTTP request. :(
William
A: 

Thanks for Sharing.. its working

Bobby