$(function() {
$("<img>", {
src: "http://goo.gl/GWtGo",
error: function() { alert("error!"); },
load: function() { alert("ok"); }
});
});
Got ispirazione da How can I test if a URL is a valid image (in javascript)?Si tratta di un test valido per verificare se un URL fa riferimento a un'immagine in JS/jQuery
UPDATE
Il prossimo passo sarà: come posso incapsulare questo logica in una funzione. Ho provato questo ->http://jsfiddle.net/wp7Ed/2/
$(function() {
function IsValidImageUrl(url) {
$("<img>", {
src: url,
error: function() { return false; },
load: function() { return true; }
});
}
alert(IsValidImageUrl("http://goo.gl/GWtGo"));
alert(IsValidImageUrl("http://error"));
});
ma naturalmente non riesce ... come posso tornare da un gestore di eventi internl? O come posso implementarlo?
Relat ed: http://maisonbisson.com/blog/post/12150/detecting-broken-images-in-javascript/ –