javascript - How does the browser determine an onerror event for an img -


i working on application serve image client, , sometime image may blank, , use spring mvc controller, core codes:

@requestmapping public responseentity<byte[]> getimg(string id) {     byte[] res = imgservice.find(id); // res  may null     headers.setdate("expires", system.currenttimemillis() + 24 * 3600 * 1000 * 7);     return responseentity.ok().headers(headers).contenttype(mediatype.image_png).body(res); } 

as shown, response code 200 no matter image exist or not.

and in client side after load image , add onerror event image, found onerror event triggered once server can not find image(which means body may null) status code 200.

then tried return fixed image once required image missing, , onerro event not triggered.

so wonder how browser think img load error, seems not based on status code.

any documents this?

adding event listner on img tag , have custom logic called.

sample code:

<img id="photo"      onload='loaded(this.id)'      src=""      alt="alt text"      /> 

there post on how detect image load errors. post 1, detecting using plain javascript , this.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -