i am changing the source of an image using the following code:
var src = "/Content/Productimg/" + data.image +".jpg";
$("#image").attr("src", src);
However, sometimes if data.img returns test and test.jpg does not exist in the Productimg folder a funny icon is displayed.
Is there a way i can check if the image is there before assigning the src?
For checking if image exists or not , you can do:
$.ajax({ url:'http://yourhost/someimage.ext', type:'HEAD', error: function() { //file does not exist }, success: function() { //file exists do something here } });