Skip to content
Related Articles

Related Articles

How to detect Adblocker using JavaScript ?

Improve Article
Save Article
  • Last Updated : 05 Mar, 2021
Improve Article
Save Article

In this article, we will be developing an adblocker detector using JavaScript. Adblocker is an extension that is used to block the ads which are served by the website. Adblocker blocks the DOM and the script which has the code to show ads. The adblockers have massive data of blocklist file names and the adblockers detect if the website is using any files from them, It restricts that file from downloading, so the ad script does not load. easylist.text is a huge list of blocklists that contains most of the adblocking file names.

According to a survey, 10% of people use adblocker for browsing with any ads. It means 10% less revenue by the website. This is sad for the websites that solely rely on ads, some of them use adblocker detecting script and restrict the user to enter the website without disabling the adblocker (But this is a bad idea from an SEO point of view).

Detect adblocker using bait script: In this method, you have to create a div with the class name ad-zone. Make the div 1px height. Now write a script that checks if the offset height of parent div becomes zero, then the user is using an adblocker. The adblocker blocked that div from rendering, so the offset height becomes zero. If the offset height of the div remains unchanged then there is no adblocker present in the browser. The reason is the class name ad-zone, the adblocker used to inspect which element has the class name that looks like an ad.

The easylist.text has a huge list of such class names.  Every adblocker has its own set of class names which will be blocked that’s why we should not rely on only one class name. Some common class names are textads, banner-ads, banner_ads, ad-unit, ad-zone, ad-space which are detected by the adblocker for an ad.



<!DOCTYPE html>
      height: 10px;
    <p id="msg">checking for adblocker...</p>
     class="ad-zone ad-space ad-unit textads banner-ads banner_ads">


let x = document.querySelector(".ad-zone");
   let x_height = x.offsetHeight;
   let msg = document.getElementById("msg")
     msg.innerText = "No Adblocker detected"
   } else{
     msg.innerText = "Adblocker detected"

Output :

  • We are running our code on and using the extension AdBlock. When the adblocker is running, the following output can be seen.

  • When the adblocker is turned off for, the following output can be seen.

Example 2: The following code can be used in any webpage under the <script> tag. 


let fakeAd = document.createElement("div");
  fakeAd.className = 
  "textads banner-ads banner_ads ad-unit ad-zone ad-space adsbox" = "1px"
  let x_width = fakeAd.offsetHeight;
  let msg = document.getElementById("msg")
      console.log("No AdBlocker Detected")
      console.log("AdBlocker detected")

Output : You can paste this code in any browser console, or you can use it in a script tag. 

Adblocker detector

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!