How to Detect an AdBlocker on Your Site Using JavaScript


Web developers often encounter the problem of ad blockers, which can interfere with a site’s functionality. One way to solve this issue is by detecting the ad blocker using JavaScript. In this article, we’ll look at a simple method for checking the activity of an ad blocker and alerting users.

How it works: We create two elements:

  1. A hidden div element that is often blocked by ad blockers.

If one of these elements fails to load or is not visible, it means the ad blocker is active, and we can display an alert to the user.

Code example:

<script>
(function() {
    var adElement = document.createElement('div');
    adElement.className = 'adsbox reklama';
    adElement.style.height = '10px';
    adElement.style.position = 'absolute';
    adElement.style.top = '-9999px';
    document.body.appendChild(adElement);

    setTimeout(function() {
        var isAdBlockActive = adElement.offsetHeight === 0;

        if (isAdBlockActive) {
            alert('Блокувальник реклами активний! Будь ласка, вимкніть його');
        }

        document.body.removeChild(adElement);
    }, 300);
})();
</script>




Conclusion: This method effectively detects ad blockers on your site and alerts users about their presence. By doing so, you can improve the user experience and ensure your content functions properly.

Leave a comment(3)

  • I’m sorry, but I constantly get the text “Блокувальник реклами активний! Будь ласка, вимкніть його.”, even when adblock is turned off
    What am I doing wrong?

    Reply
    Posted on: 19.11.2025
    • <script>
      (function() {
          var adElement = document.createElement('div');
          adElement.className = 'reklama'; // название, которое блокируют
          adElement.style.height = '1px';
          adElement.style.width = '1px';
          adElement.style.position = 'absolute';
          adElement.style.top = '-9999px';
          adElement.style.left = '-9999px';
      
          var adImage = new Image();
          adImage.src = "/ads/reklama.jpg"; // путь, похожий на рекламу
      
          document.body.appendChild(adElement);
          document.body.appendChild(adImage);
      
          setTimeout(function() {
              var isAdBlockActive = false;
      
              if (adElement.offsetHeight === 0) {
                  isAdBlockActive = true;
              }
      
              if (adImage.height === 0) {
                  isAdBlockActive = true;
              }
      
              if (isAdBlockActive) {
                  alert('Блокувальник реклами активний! Будь ласка, вимкніть його.');
              }
      
              document.body.removeChild(adElement);
              document.body.removeChild(adImage);
          }, 1000);
      })();
      </script>
      
      Posted on: 19.11.2025
    • I’ve added 100% working code to the article. Please review the changes.

      Posted on: 20.11.2025

Leave a Reply

Your email address will not be published. Required fields are marked *