Home > database >  Alert if multi text found in javascript
Alert if multi text found in javascript

Time:07-20

I have this code that alert if found a word, how can I make this work with multi words?

window.addEventListener('load', function() {
    
  setTimeout(function(){
        console.log("Page loaded.");
      var LookFor = "HELLO"; // Text to find
        var content = document.body.textContent || document.body.innerText;
        var hasText = content.indexOf(LookFor) !== -1;
        if(hasText) {
           console.log("Text fond!.");
          alert(Text fond)
      } else {
           console.log("Text not fond, reloading.");
            location.reload();
        }
   },3000);
});

I mean something like:

var LookFor = "HELLO","World","Fox","Number";

I try many way but don't work, anyone can help me?

CodePudding user response:

You will need to create an array and do forEach like this:

window.addEventListener('load', function() {
    
  setTimeout(function(){
        console.log("Page loaded.");
var lookFor = ["HELLO","World","Fox","Number"];
lookFor.forEach(word => {

var content = document.body.textContent || document.body.innerText;
        var hasText = content.indexOf(word) !== -1;
        if(hasText) {
           console.log("Text fond!.");
// This will alert only found words
          alert("Text found: "   word);
      } else {
           console.log("Text not fond, reloading.");
            location.reload();
        }
 
});
        
   },3000);
});

  • Related