Advertisement
yesamarcos

Verificar se imagem existe ou não

May 11th, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Verificando se a imagem existe ...
  3.  * @param  {[type]} imageUrl [description]
  4.  * @param  {[type]} callBack [description]
  5.  */
  6. function checkImageExists(imageUrl, callBack)
  7. {
  8.     var imageData = new Image();
  9.     imageData.onload = function() {
  10.         callBack(true);
  11.     };
  12.     imageData.onerror = function() {
  13.         callBack(false);
  14.     };
  15.     imageData.src = imageUrl;
  16. }
  17.  
  18. let foto = "https://palhocasites.com.br/webview/images/fornecedores/capita-marvel.jpg"
  19. checkImageExists(foto, function(existsImage)
  20. {
  21.     if(existsImage == true) {
  22.         console.log('imagem')
  23.     } else {
  24.         console.log('sem imagem')
  25.     }
  26. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement