javascript - How to detect if the image path is broken? -
i trying detect if image available in javascript
.
i have switch statement return different type of images.
switch (type){ case 'oldproduct': return "<img src='project/'" + folder + imagename + "/>" break; case 'newproduct': return "<img src='project/'" + folder2 + imagename2 + "/>" break; more cases.... }
i wondering if there anyways detect if images exist before return image. <img src='project/'" + folder2 + imagename2 + "/img> src
broken path in case. lot!
if wanted use javascript , not jquery suggestion load image onto page
<img src="yoursrc" id="testimage" style="display:none;"/>
then check onerror method "not may not work in old ie"
var image = document.getelementbyid('testimage'); im.onerror = function(){ //do on error };
however neater , reliable way run jquery on image.
like
function checkimg(src){ var jqxhr = $.get(src, function() { return true; }).fail(function() { return false; }); }
Comments
Post a Comment