Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check if a image file exists with Javascript
/**
* Checking if an image exist in your image folder
*/
let loadImage = function(variable){
var image = new Image();
var url_image = './ImageFolder/' + variable + '.jpg';
image.src = url_image;
if (image.width == 0) {
return `<img src='./ImageFolder/defaultImage.jpg'>`;
} else {
return `<img src='./ImageFolder/`+variable+`.jpg'`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment