I am using codeigniter(PHP) for creating a web. I am loading data with jquery ajax and there is a image field in data array. Now i want to check if image is exists or not on server. How can i do this with jquery or javascript. My file location is :-
var path = 'C:\\wamp\\www\\project\\uploads';
if(val.default_image == null)
{
var url = '<?php echo site_url().'uploads/default.png'; ?>';
var style = 'background-color: rgb(246, 95, 13);height: 200px';
}
else if(checkImage(path+'\\'+val.default_image))
{
$style = 'backgound-color:none;height: 200px';
var url = '<?php echo site_url()."uploads/";?>'+val.default_image;
}
And i am trying this function:-
function checkImage(src) {
var img = new Image();
img.onload = function() {
alert('yes');
return true;
};
img.onerror = function() {
alert('no');
return false;
};
}
But this function is not working properly. Is there any better solution to check or this?