Last active
January 1, 2023 03:30
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); |
@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
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.