Skip to content

Instantly share code, notes, and snippets.

@pabloleonalcaide
Created May 17, 2018 08:08
Show Gist options
  • Save pabloleonalcaide/1e8ece835ea689894dd37a44f0ce7134 to your computer and use it in GitHub Desktop.
Save pabloleonalcaide/1e8ece835ea689894dd37a44f0ce7134 to your computer and use it in GitHub Desktop.
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