I have a lot of images in my folder. I use this function for each of them:
export function imageExist(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status != 404;
//OR
var img = new Image();
img.src = url;
return img.height!=0;
}
But all these fails appear in "networks" tab of the explorer:
Request URL: http://127.0.0.1/images/p_14047_0.jpg
Request Method: GET
Status Code: 404 Not Found
I am not sure but I guess it affects a lot the performances, somethimes I have about 150images in the page.
Here is how I use it, maybe there is a better way to find out if an image exists, if yes return it, otherwise return a default image.
export function getLogo(id)
{
var img = new Image();
var url = '/images/p_' + id + '.jpg';
img.src = url;
if(imageExist(url)) return url;
var url = '/images/p_' + id + '.jpeg';
img.src = url;
if(imageExist(url)) return url;
var url = '/images/p_' + id + '.png';
img.src = url;
if(imageExist(url)) return url;
else return '/images/default.jpg';
}
Thanks a lot for your help!