0

Can someone please help me with this script? It is to detect adblock. I have <script src="/js/ads.js"></script> in the head (a empty ads.js in the folder). Adblock will block this from loading thus not being on the page. Then I have the code below that will detect if the script is loaded or not. For some reason it is not working properly and still displays images. I had someone write the script below as well for it to check for ads 3 times with a 1 second interval but it seems to check infinitely 3 times at once. Can someone please help me work this properly? And also if it detects that it does load properly it won't keep pasting images into the div?

<script>
$(document).ready(function () {
    var count = 3;
    for (var i = 0; i < count; i++) {
        setInterval(function () {
            if (window.canRunAds === undefined) {
                $('#StEQBidTjU').prepend('<img src="/miscimg/mZKoARJXcF.jpg" id="PtZZtkYjaR" />')
                $('#AbHPbbbxyl').prepend('<img src="/miscimg/6hZ4nqcBZd.jpg" id="PLyCMzOHpx" />');
            }
        }, 1000);
    }
});
</script>
Barmar
  • 741,623
  • 53
  • 500
  • 612
Lion
  • 121
  • 1
  • 10
  • *"it seems to check infinitely"* - The `setInterval()` function will call the function you pass it repeatedly - that's what it is supposed to do. Use `setTimeout()` instead. – nnnnnn Sep 24 '15 at 20:56
  • `ads.js` shouldn't be empty, it should contain `var canRunAds = true;`. See http://stackoverflow.com/questions/4869154/how-to-detect-adblock-on-my-website – Barmar Sep 24 '15 at 20:56
  • @nnnnnn my knowledge of this is REALLY basic like I pretty much know nothing. Could you help me and tell me what to change specifically? – Lion Sep 24 '15 at 21:07
  • @barmar thank you very much! I hadn't noticed that part. Really appreciate that! – Lion Sep 24 '15 at 21:08

2 Answers2

1

You need to keep track of the count in each interval, and clear it once it's ran 3 times.

$(document).ready(function () {
    var count = 3,
        interval = setInterval(function () {
          if (--count < 0) {
            clearInterval(interval);
          }
          if (window.canRunAds === undefined) {
            $('#StEQBidTjU').prepend('<img src="/miscimg/mZKoARJXcF.jpg" id="PtZZtkYjaR" />')
            $('#AbHPbbbxyl').prepend('<img src="/miscimg/6hZ4nqcBZd.jpg" id="PLyCMzOHpx" />');
          }
        }, 1000);
});
Neil S
  • 2,304
  • 21
  • 28
  • This works awesome, but it now pastes the image 4 times. Is there any way I could have it replace maybe if there already a image there or something to solve it placing it 4 times? – Lion Sep 24 '15 at 21:15
  • @Lion well, you're prepending to an element that I don't know anything about. Could you perhaps just overwrite the contents each time (using the html method instead of prepend)? – Neil S Sep 24 '15 at 22:21
  • I don't really know anything about jquery or anything could you explain what you mean with html method? The element that I am prepending is just a simple div with the heights of 300x600 and 728x90, and I am inserting these 2 images that are the same height. Apart from adverts that are meant to display there, there is nothing else in them. If the ads don't display the images get inserted that's about it. – Lion Sep 24 '15 at 22:46
  • you are using the prepend method - http://api.jquery.com/prepend/ - which is _adding_ the contents you are passing(the img elements) to the element in the selector (e.g. '#StEQBidTjU'). Instead of adding, you could use html() - http://api.jquery.com/html/#html2 - to _replace_ the contents of the element in the selector with the contents you are passing. – Neil S Sep 24 '15 at 22:49
  • 1
    This is awesome thank you! It works perfect. I really appreciate all the help guys! – Lion Sep 24 '15 at 23:02
0

Now you don't even need to do all these to detect AdBlock users, You can achieve this using a simple JS script called ABDetector
Here's how to use it:
- Download/Clone the project, upload the file abDetector.min.js
- Put this in your <head>:
<script type="text/javascript" src="abDetector.min.js"></script>
- Use this wherever you want to display a message to AdBlock users:
<div id="ab-message" style="display: none">Your message here!</div>

Then you're done. Check out the project on Github.

Nick Rameau
  • 1,258
  • 14
  • 23