Reputation: 1728
I've run into a dilemma with jQuery's onerror handling (or more specifically the browser's javascript onerror event). I've got an image handler which loads an image from the database, and if it isn't found it returns a 404 status code but also a default not found image.
It turns out that unless the content body is empty, the browser doesn't fire the onerror event. jQuery states this is based on the response status code, but this must not be true. Anyone know of a way around this?
Upvotes: 4
Views: 5634
Reputation: 17909
I typed a long answer but my browser chewed it ... have a look at the following article and see what happens if you bind OnError to your images after the DOM has loaded:
http://maisonbisson.com/post/12150/detecting-broken-images-in-javascript/
Be wary though as the method you're trying to achieve is a workaround something that's actually a valid behaviour.
Instead of returning a 404 and then a default image, why don't you simply just return the default image and an OK, if your image handler can't find the requested one ? After all, even though it's not the specific image you need, it's still a valid, albeit default, response.
Upvotes: 1