Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ā¤ šŸ’»
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

JavaScript - how to detect if adblock is enabled?

0 contributions
6 points

In this article, we're going to have a look at how to detect if ads are blocked in JavaScript. By default there is no any universal variable that indicates that,Ā ads are blocked or not.

There are tricks how tomake adblock test:

  • check if ads scripts are loaded correctly (onload andĀ onerror events);
    example path:
    • https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js
  • check if ads elements areĀ mounted correctly in DOM,
  • try to load some fake js file that pretendsĀ advertisements and check if is loaded or not - this approach is universal;
    example paths:
    • /js/ads.js,
    • /scripts/advertisement.js,
  • try to create some fake element that pretendsĀ advertisements and check if is blocked - this approach is universal.

Check below example to see how it works:

1. Fake advertisiement script test example

If final status is equal to 0 it means, it means adblock is enabled.

Note: this approach is universal.

// ONLINE-RUNNER:browser;

function checkAdsBlocked(callback) {
  	var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function () {
        if (xhr.readyState == XMLHttpRequest.DONE) {
          	callback(xhr.status == 0);
        }
    };

    xhr.open('GET', '/scripts/advertisement.js?' + Math.random(), true);
    xhr.send(null);
}

// Usage example:

checkAdsBlocked(function(adsBlocked) {
    console.log('adsBlocked: ' + adsBlocked);
});

2. FakeĀ advertisements element test example

If height of created fake element will be 0 it means adblock is enabled.

Note: this approach is universal.

// ONLINE-RUNNER:browser;

function checkAdsBlocked(callback) {
    var test = function() {
        var hAds = document.createElement('div');

        // we dont want to see fake element on page
        hAds.style.position = 'absolute';
        hAds.style.left = '-100px';
        hAds.style.top = '-100px';

        // class="adsbox" causes blocking content
        hAds.className = 'adsbox';
        hAds.innerHTML = ' ';

        document.body.appendChild(hAds);

        setTimeout(function(){
            callback(hAds.offsetHeight == 0);

            document.body.removeChild(hAds);
        });
  	};

    if (document.body) {
      	test();
    } else {
      	window.addEventListener('onload', test);
    }
}

// Usage example:

checkAdsBlocked(function(adsBlocked) {
    console.log('adsBlocked: ' + adsBlocked);
});

Ā 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey šŸ‘‹
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more