jQuery: Check if image exists

后端 未结 6 691
予麋鹿
予麋鹿 2020-11-27 17:20

I\'m loading an image path via jQuery $.ajax and before showing the image I\'d like to check if it in fact exists. Can I use the image load/ready event or something similar

6条回答
  •  春和景丽
    2020-11-27 18:22

    An old thread I know but I think it could be improved. I noticed OP having intermittent problems which could be due to loading of the image and error checking simultaneously. It would be better to let the image load first and then check for errors:

    $(".myimage").attr("src", imagePath).error(function() {
        // do something
    });
    

提交回复
热议问题