Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

javascript - How to get HTTP status code of <img> tags

I have a page with a lot of images that are generated server-side depending on user actions. And when image loads successfully I'm happy, but when there is an error on the server I have to act according to what an error occurred.

For example:

  • 500 code: do this stuff.
  • 503 code: do that stuff

and so on.

So, my question: is there any way to get status code within "img" tag on-error event handler?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No, there is no way to get the HTTP status from a request made by an img tag in JavaScript.

You could write a firefox plugin, chrome/safari extension to do that.

An alternative would be using AJAX to load your images (not using img tags). You can get Http Status Codes from Ajax Requests.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...