Languages
[Edit]
EN

JavaScript - detect if adblock is enabled (tested in 2024)

6 points
Created by:
Mikolaj
519

In this article, we would like to show how using JavaScript in simple way detect if some adblock is enabled in web broswer.

Note: the article contains solutions proposed and tested at 03:07 AM in 2023-01-01 (It was my New Year scarify for humanity - Root-ssh).

 

Problem overview

Edit

By default there is no any universal API that indicates ads are blocked or not.

There are tricks how test if adblock is not used:

  • check if ads scripts are loaded correctly,
    example path:
    • https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js
  • check if ads elements are mounted correctly in DOM (it requires multicase tests).

Note: many solutions that were working in the past stopped working, so they are not presented in the above solutions, so we focus on proper solutions only.

 

Example solution

Edit

In this section, you can find solution that works for:

Source code:

Where truth of:

  • xhr.status === 0 means AdBlock is used,
  • xhr.responseURL !== ADS_URL means uBlock Origin is used.

 

ES6+ version

Edit

In this section, you can find above solution in ES6+ version.

 

Alternative titles

  1. JavaScript - detect if adblock is disabled (tested in 2024)
  2. JavaScript - detect if ads are blocked (tested in 2024)
  3. JavaScript - detect if some adblock is used (tested in 2024)
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join