okel okel - 1 month ago 8
Javascript Question

Trigger an ad for the first click on ANY part of the website

There are some websites where no matter where I click (background, random words, existing links, anything) an ad pops up. For example: first time I click Contact I get the Spam, but if I click Contact again I will actually go to "/contact" section.

I wonder how is this possible and how it is made.

Answer

Like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script> 
i = 0;

$(document).on('click','body',function(event){

  if (i == 0) {
    event.preventDefault();
    //insert code to display ad here
    alert('spam here');
    i++;
    }
  });
  </script>
  

<body>
<div>blah blah blah</div>
<a href = "//stackoverflow.com">link here</a>
  </body>

After the first click, i is no longer = 0, therefore the default action of body elements is restored.

Comments