Search code examples
javascriptjquerysetintervaltampermonkey

Return False From Clicking


im Trying to Make the Click dont do the Work if there is a word (Hello) on the Website But if there isn't a word Hello then will Keep Click on the iD(safe)

if ((document.body.innerText).indexOf('Hello') == 1)

{
    return false;


    $('#safe').trigger('click');


}


Solution

  • <html>
      <p>Hello</p> <!-- remove this line to see what if no Hello on page -->
    
      <button type='button' id='safe' onclick="isHelloThere()">click me</button>
    
      <script type='text/javascript'>
        function isHelloThere() {
          if ((document.body.innerText).indexOf('Hello') > -1) {
            // do nothing
          } else {
            alert('I do not see a Hello word on the page.')
          }
        }
      </script>
    </html>
    

    You probably need to update your questions with more details. Anyway, according to your comments, I guess this is what you need.

    You can create a click function then determine if there is Hello on the page in your click function. https://jsfiddle.net/jialinzou/cztmusLr/28/