Skip to content

Instantly share code, notes, and snippets.

@draeton
Last active January 1, 2023 03:30
Show Gist options
  • Save draeton/1210357 to your computer and use it in GitHub Desktop.
Save draeton/1210357 to your computer and use it in GitHub Desktop.
Check the existence of an image file at `url` by creating a temporary Image element.
// Example Use
(function () {
function success() {
console.log("success: ", this.src);
}
function failure() {
console.log("failure: ", this.src);
}
// this fails
checkImage("http://www.google.com/", success, failure);
// this succeeds
checkImage("http://www.google.com/intl/en_com/images/srpr/logo3w.png", success, failure);
})();
(function (window) {
// Stores past URLs that failed to load. Used for a quick lookup
// and because `onerror` is not triggered in some browsers
// if the response is cached.
var errors = {};
// Check the existence of an image file at `url` by creating a
// temporary Image element. The `success` callback is called
// if the image loads correctly or the image is already complete.
// The `failure` callback is called if the image fails to load
// or has failed to load in the past.
window.checkImage = function (url, success, failure) {
var img = new Image(), // the
loaded = false,
errored = false;
// Run only once, when `loaded` is false. If `success` is a
// function, it is called with `img` as the context.
img.onload = function () {
if (loaded) {
return;
}
loaded = true;
if (success && success.call) {
success.call(img);
}
};
// Run only once, when `errored` is false. If `failure` is a
// function, it is called with `img` as the context.
img.onerror = function () {
if (errored) {
return;
}
errors[url] = errored = true;
if (failure && failure.call) {
failure.call(img);
}
};
// If `url` is in the `errors` object, trigger the `onerror`
// callback.
if (errors[url]) {
img.onerror.call(img);
return;
}
// Set the img src to trigger loading
img.src = url;
// If the image is already complete (i.e. cached), trigger the
// `onload` callback.
if (img.complete) {
img.onload.call(img);
}
};
})(this);
@sfentress
Copy link

Very nice. But what's the point of checking for errors[url] after setting the img.src? It seems as if that will make the browser go ahead and try and download the image again for no reason, if you know you're just going to error out if you've already discovered it doesn't exist. My small change therefore (using this in my own code) was to swap lines 50-52 with line 46, and return after "img.onerror.call(img);", to hard-abort and give up instead of forcing the browser to make another request.

@draeton
Copy link
Author

draeton commented Feb 12, 2013

Thanks for this. That was an oversight on my part.

@alanmilinovic
Copy link

Nice and simple solution!

If error occur, is it possible to get error message like error 404 (Not Found) or 401 (Authorization Required)?
I can read error in console log, but is it possible to display error on web page or store it in database for later use...

Thank you in advance.

@sfentress
Copy link

@Amilino: as far as I know, img.onerror doesn't provide you with useful information such as the response code from the server. To do that, I think you would need to do an AJAX request for the image. This would give you the complete response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment