$(function() {
$("<img>", {
src: "http://www.google.com.sg/intl/en_ALL/images/logos/images_logo_sm.gif",
error: function() { alert("error!"); },
load: function() { alert("ok"); }
});
});
Got inspiration from How can I test if a URL is a valid image (in javascript)?
UPDATE
The next step will be: how can I encapsulate this logic into a function. I tried this -> http://jsfiddle.net/wp7Ed/2/
$(function() {
function IsValidImageUrl(url) {
$("<img>", {
src: url,
error: function() { return false; },
load: function() { return true; }
});
}
alert(IsValidImageUrl("http://www.google.com.sg/intl/en_ALL/images/logos/images_logo_sm.gif"));
alert(IsValidImageUrl("http://error"));
});
but of course it fails... how can I return from an internl event handler? Or how can I implement this?