Follow us on - Dribbble | Twitter | Facebook

Detecting Adblocker with Javascript

adblocker detector

Now-a-days most of the readers installed adblocker plugin which blocks annoying ads in the browser. But from the publisher’s point of view adblocker is killing the revenue of the website. Most of the publisher share their content for free of cost and gets revenue from displaying ads to pay for hosting, domain, emails, writers and other services. Let see how to detect adblocker with javascript.

The advertisement is the only way to get more revenue to run the blog. Due to adblocker, they may get fewer page views which lead to getting less revenue.

Using simple javascript code can able to remove Adblock plugins. Some ways to remove adblockers are:

  1. By hiding the content.
  2. Disable download button.
  3. Show a simple pop message/banner at the top.

From the above, the method readers have a chance to bounce from your website. so you need to give more useful content.

Below Code is work for google adsense and some other ads providers also. Let see how it works…

For Google Adsense:

In below code load adsbygoogle.js, If adblocker is installed, blocks the js and call onerror event ->  checkJSerror()
Add script tag in index.html

<script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js” onerror=”checkJSerror();”></script>

Add snippet in index.html – Event trigger if adsbygoogle.js is not loaded.

function checkJSerror(){
var elm = document.createElement(“div”);
elm.id = “checkaud”;
elm.className = “checkaud”;
elm.innerHTML = “Please disable the ad blocker.”;
document.body.appendChild(elm);
}

In checkJSerror, Show message using popup or banner.

Common Way to detect adblocker:

Include ads.js in index.html

<script src=”ads.js” type=”text/javascript”></script>

Add snippet in ads.js

var elm = document.createElement(“div”);
elm.id = “checkaud”;
document.body.appendChild(elm);

Add snippet in the index.html file.

window.onload = function(){
var checkaud = document.getElementById(“checkaud”);
if(checkaud){
alert(“no ad blocker is installed in browser”)
}
else{
alert(“please dispble ad block”)
}
}

If ads.js is load successfully, then checkaud element is present. So adblocker is not installed.
If ads.js is not loaded, show message/banner to remove adblocker.

You may also like