How to detect ad blockers on web sites?

Most Ad blockers disable communication between your client and ad servers. These filter lists are essentially an extensive set of rules, which tell which elements of the pages to block. A common rule consists in blocking all URLs containing the word "ads". This is what is used here to detect if ads are blocked.

Create a file named ads.js containing the following script:

var canRunAds = true;

Load the script in the header of each page where blockers need to be detected:

<head>
    ...
    <script src="/js/ads.js"></script>
</head>

Most blockers should prevent this script from being loaded. So you now just have to detect if the variable canRunAds exists with the following JavaScript:

if( window.canRunAds === undefined )
{
    // adblocker detected, do something
    displayAlternateMessage();
}

See also


Last update : 04/13/2019