turnley.dev

PHP and MySQL code for faucet operators

Tools & Calculators

FRD-008

Detect an ad blocker without picking a fight

A bait element and one class toggle. Ask, do not block — a wall costs you the visitor and the search ranking.

Blockers work by hiding elements whose class or filename matches a list. That gives you a simple test: put an element with one of those names on the page, and if it has zero height after load, something removed it.

What you do next decides whether this is worth having. A modal that blocks the page loses the visitor, and on a content site it loses the ranking that brought them. A quiet line explaining that banners pay for the library converts a surprising number of people on a site they find useful.

On a library aimed at developers, expect the blocker rate to be high. That is a reason to keep the message calm, not a reason to escalate.

JS
class="tk-c">// bait.js — served from your own domain under an unremarkable name,
class="tk-c">// so that the FILE is not blocked before it can run.
(function () {
    var bait = document.createElement(class="tk-s">&class="tk-c">#class="tk-n">039;div&#class="tk-n">039;);
    bait.className = class="tk-s">&class="tk-c">#class="tk-n">039;ad-banner ads adsbox ad-placement pub_300x250&#class="tk-n">039;;
    bait.style.cssText = class="tk-s">&class="tk-c">#class="tk-n">039;position:absolute;left:-9999px;top:-9999px;width:300px;height:250px;&#class="tk-n">039;;
    document.body.appendChild(bait);

    window.setTimeout(function () {
        var hidden = bait.offsetHeight === class="tk-n">0
                  || bait.offsetParent === null
                  || window.getComputedStyle(bait).display === class="tk-s">&class="tk-c">#class="tk-n">039;none&#class="tk-n">039;;

        document.documentElement.classList.toggle(class="tk-s">&class="tk-c">#class="tk-n">039;blocked-ads&#class="tk-n">039;, hidden);
        bait.remove();
    }, class="tk-n">250);
}());

Using it

Serve the script from your own domain with a neutral filename. Anything called ads.js is blocked before it executes and the test never runs.

Style the notice with .blocked-ads .ad-notice { display:block } so it costs nothing when there is no blocker.

Keep the wording factual: what the ads pay for, and that nothing on the site is gated.

What bites people

The check needs a delay. Run it immediately and the blocker has not acted yet, so you get a false negative.

Never gate content on the result. On a site that lives on search traffic, a blocking overlay is the most expensive element you can add.

Also in Anti-Bot and Fraud