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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -