How to search image using javascript?

Asked

Viewed 119 times

0

I wrote a code that looks for a specific text on a page and if it contains returns positive if it does not update. But I wanted to improve the code and make you look for lines in the source code of the page to find a specific image like this

src="https://i.dstatic.com/images/glyphish/837-Palette-Selected.png"

$(document).ready(function() {
    setTimeout(function() {
        var LookFor = "texto "; // procurar por
        if ($('body:contains("' + LookFor + '")').length > 0) {
            alert("Achou " + LookFor);
            return;
        } else {
            location.reload();
        }
    }, 2000); // troca pra aumentar o tempo
});
  • $('img[src="'+url+'"]'), that’s what you want?

  • It worked, thank you very much.

1 answer

0

See if it helps you?

Anything goes down there commenting that we adapt the code.

function verifica() {

  if ( $('img[src="https://i.dstatic.com/images/glyphish/837-palette-selected.png"]').length > 0 ) {
      console.log('Achou. Pára de procurar');
      alert('Encontrou!');
    } else {
      console.log('Não achou. Procura novamente depois do intervalo definido');
      setTimeout( verifica, 2000 );
    }
    
}

$(function() {

  verifica();

  $('button').on( 'click', function(){
  
      $('body').append('<img src="https://i.dstatic.com/images/glyphish/837-palette-selected.png">');
    
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<button>Adiciona Imagem</button>

  • Oops, I got it with Guilherme’s comment up there, but thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.