Friday, 15 June 2012

javascript - Checking if an image has an error? -



javascript - Checking if an image has an error? -

i create image:

var img = new image(); img.src = filesrc; img.onload = function() { };

is there way check if there error image?

i've tried:

img.error(function() { console.log("error loading image"); });

but error:

uncaught typeerror: undefined not function

try image onerror event

inline ex.

html <img src="example.png" onerror="imgerrorfunction(this)" /> js function imgerrorfunction(obj) { obj.src = '/404.png'; } edit:

didn't realice utilize image in dinamic function... error must catched @king king suggest (same event).

var filesrc = 'example.png'; var obj = new image(); obj.src = filesrc; obj.onerror = function() { obj.src = '/404.png'; // must exist avoid error loop };

javascript jquery

No comments:

Post a Comment