Tom
Tom

Reputation: 7586

How to detect when an image link returns 404, BUT it's still a valid image

Youtube returns 404 for not existing thumbnails, but it also returns valid image data (broken video thumbnail), so checking it with Image does not work, onerror is not called:

var img = new Image();
img.onload = function() { alert("found")};
img.onerror = function() { alert("not found") };
img.src = "http://img.youtube.com/vi/aaaa/1.jpg";

When run then it says "found". Is there a way to detect 404 if the image data can actually be loaded?

It is also good if it shomehow can be detected that the link returns the standard youtube "broken video" thumbnail image data.

Upvotes: 8

Views: 2387

Answers (1)

Jan Paepke
Jan Paepke

Reputation: 2027

As a crude but working workaround in this case I would suggest checking the image's size on load. Usually the thumbnails are bigger, so if the returned image is 120x90px you can assume it's the 404 image.

The only other way will be to have it load via a PHP script that can check the HTTP Headers before passing through the image.

Upvotes: 4

Related Questions