jquery - Check if an image is loaded (no errors) in JavaScript -


i'm using javascript jquery library manipulate image thumbnails contained in unordered list. when image loaded 1 thing, when error occurs else. i'm using jquery load() , error() methods events. after these events check image dom element .complete make sure image wasn't loaded before jquery register events.

it works correctly except when error occurs before jquery can register events. solution can think of use img onerror attribute store "flag" somewhere globally (or on node it's self) says failed jquery can check "store/node" when checking .complete.

anyone have better solution?

edit: bolded main points , added detail below: i'm checking if image complete (aka loaded) after add load , error event on image. way, if image loaded before events registered, still know. if image isn't loaded after events events take care of when does. problem is, can check if image loaded already, i can't tell if error occurred instead.

another option trigger onload and/or onerror events creating in memory image element , setting src attribute original src attribute of original image. here's example of mean:

$("<img/>")     .on('load', function() { console.log("image loaded correctly"); })     .on('error', function() { console.log("error loading image"); })     .attr("src", $(originalimage).attr("src")) ; 

hope helps!


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -