More information on the reason for the JavaScript Image onerror

I am creating an image in JavaScript to load the image, including providing an onerror function in case something goes wrong. In this case, I would like more information about the cause of the error, for example, there is no image or there is a problem with the network, security limitation, ...

On the server side, I'm not just serving static images, I'm actually using PHP to do some validation. Ideally I would like my server to send the image if possible, but if not, please provide some custom text message.

The onerror () function doesn't seem to provide any additional information other than firing or not. I can ask the server for more information in a separate call, but my code will be much simpler if it can access the information already sent and received.

Can I get more information (ANY information) about the reason for shelling onerror ()? Can I submit a custom error message from PHP that JavaScript can access?

+3


source to share


1 answer


To answer my own question, I believe that the comment left by adeneo is correct - the onerror method doesn't contain the information I want, so I wouldn't want to work. However, CBRoe's proposal to provide an alternative image got me thinking.

Attention! Ugly hack:



I am supplying different images depending on the "error message" the server would like to send. They can be displayed, or how JavaScript can parse images. Huge amounts of data can be encoded as pixel data, but reading is a pain. Instead, for my limited number of error conditions, I'm pretty sure each image has a different width and / or height from each other and from the valid images (for my application, valid images have a minimum height), so I can quickly determine which one was posted. and what error condition it signals.

I don't recommend this, but it's a quick fix for my situation.

0


source







All Articles