How To: Detect Ad Blockers (adblock, uBlock, etc)

As a blogger or any other kind of publisher, you might be interested in if the user accessing your page right now is using an ad blocker or not. You might want to do that to display a box that encourages them to disable their ad blocker, because you’re trying to make a living off of blogging or to make more conscious decisions about how you’re going to layout your page.

If a user has an adblocker installed, you don’t have to reserve the space in your layout for the ad units, so you might as well get rid of them, so in this post we’re going to have a look at how we can determine if a user has installed an ad blocker like Adblock, Adblock Plus or uBlock.

Disclaimer: Some countries have discussed if it is legal for a webmaster to determine if their users have an adblocker installed or not, because it technically is a method of figuring out what software a user has installed, which quickly becomes a privacy matter.

How do Adblockers work?

Basically Adblockers are a huge blacklist of which files not to load or from which domains NOT to load files. This goes for Brave, Adblock, Adblock Plus and uBlock as well.

So when you open a page, this adblocker looks at what (mostly JavaScript files) and domains are on its blacklist.*

and every time your browser will try to load one of these scripts or from one of the listed domains, the blocker will stop your browser from loading that and as a result, the ads will not be loaded and also not displayed.

More important than not seeing the ads for most users is that they will not be tracked and no major company will know that they looked at a new swim suit and therefor spam them with swimsuit ads for the rest of the year.

You can have a look at a huge collection of blacklists for adblockers at

In your browser you can see if your plugin has been active by looking at your JavaScript console in the developer tools:

chrome devtools blocked request

Note the message: Failed to load resource: net::ERR_BLOCKED_BY_CLIENT, which is caused by your adblocker.

Vanilla JS Adblock / ublock Detection

If you want to know if a user has an ad blocker installed, it’s quite trivial with a few lines of JavaScript. You’ll just need an example script or file to load that you know will be blocked, if an adblocker is installed.

Let’s try to write a JavaScript function that tries to load a script we know will be blocked. Try enabling and disabling your adblocker to test if it works properly:

See the Pen Detect Adblockers by Jonathan M. Hethey (@jonathanmh) on CodePen.

For this example I picked: which is the default JavaScript file for Google Adsense.

This is the difference in page load when using GET vs HEAD, as you can see the HEAD request is significantly smaller / faster:

  • 25.1KB by using GET as the method
  • 70B by using HEAD as the method

If the current user has an adblocker installed it should read: ads are blocked else: ads are not blocked.

If you have any use cases for this, any comments or questions, leave them below or tweet at me!

Full code:

document.addEventListener('DOMContentLoaded', init, false);

function init(){
      document.getElementById('result').innerHTML = 'ads are blocked';
    } else {
      document.getElementById('result').innerHTML = 'ads are not blocked';

function adsBlocked(callback){
  var testURL = ''

  var myInit = {
    method: 'HEAD',
    mode: 'no-cors'

  var myRequest = new Request(testURL, myInit);

  fetch(myRequest).then(function(response) {
    return response;
  }).then(function(response) {

Thank you for reading! If you have any comments, additions or questions, please leave them in the form below! You can also tweet them at me

If you want to read more like this, follow me on feedly or other rss readers

19 thoughts on “How To: Detect Ad Blockers (adblock, uBlock, etc)”

    1. I would believe it works for generally all adblocker plugins, because it looks at the result of the network request

  1. Websites the aggressively try to bypass my ad blocker lose more trust. Try to circumvent my security, and I’ll make sure your site never shows me ads.

    It’s that simple. It’s time for better ads, stop trying to smuggle bad ones through our networks.

    Also, publishing bypasses makes it easier for us to block your ads.

      1. I read the post but I agree with . Although free sites deserve to be paid, all that said isn’t the same for sites, free or not, that impose obtrusive ads, spams and roadblocks on a user’s device(s). If you had a credit card eligible for online buying (unlike me) who would you you donate money to? The polite one (they only ask you, without detecting, and in a non-obtrusive way, if you would avoid the use of ad blocking but notices that you have your rights too, or simply doesn’t mind at all) or the less polite one, maybe even rude (they have a strict “law”, where you are not “authorized” to use the services on their “property” unless you avoid the adblockers)?
        IMPORTANT NOTE EVERYBODY, I’M NOT THE ONLY PERSON SAYING THIS : The only thing that could reduce the number of ad blockers is to promise and apply the exclusive use of reasonable ads, as in not annoying to any users. How difficult is that?! If a webmaster is too lazy to verify and take responsability for the ads he accepts, and attempts to hack someone else’s computer to monitor if they’re using adblock, he/she shouldn’t be shocked or find this unlawful if they see an ad blocker on their site. Anyway, adblock detection may be unlawful themselves in certain countries.

        1. I don’t think we agree on what qualifies as “hacking a computer”.

          Thank you for your comment, but it lacks a clear message or purpose.

        2. The problem with adblockers are two:

          1) The blocking extensions / browsers don’t distinguish between your perfectly OK blog with a very reasonable, non-obtrusive and properly targeted ads and a site that goes right over the top.
          The chances are your visitors have installed an adblocker because of a completely different site, but your income is suffering. Only a negligible amount of adblocker users care about issuing exceptions to sites they like.

          2) the most popular adblockers (Adblock and Adblock Plus) are owned / maintained by a single company Eyeo gmbh (notice the extremely similar layout of the options page of both extensions). Eyeo not only provides the extension, but also a private filter list (that is enabled by default). And their business model is basically “whitelisting” publishers, if they use their acceptable ads and pay a fee.
          The acceptable ads program on its own is probably best described as a scam – – the most recent posts are from December 2017.

    1. The author explicitly noted that it’s about detection.

      That said, the solution could certainly be used to serve more aggressive ads to visitors, but those ads would need to be properly masked (using random element structures, automatic javascript file obfuscation and rotation, plus the advertisements would need to be served from the current domain).

      One concern I have with the article is that requesting that Google’s ad URL could potentially expose visitors’ data to that corporation.
      A list of alternatives would certainly be useful.

    1. Brave browser doesn’t “block” unwanted requests. Rather it fakes an empty 200 OK response.
      So you have to use some different techniques to detect Brave.

  2. hi there jonathan!
    i tried to put this script on my blog page.
    but it doesn’t work.
    i’m not programmer, so maybe can you help me?
    i work on my blog page here:
    and after putting your code on my html page it says Content is not allowed in prolog (first line of your code).

    1. I bet your blog layout doesn’t allow javascript, maybe try a simple html page or a regular webhost.

  3. This is working with firefox.

    But not with chrome + adblock :

    “Uncaught TypeError: Cannot read property ‘addEventListener’ of null”

    Any idea please ?

    1. Hi Kelly,
      If using GTM, you can add the whole script without the event listener and trigger it on DOM ready. The script will then need to be:
      1. define the adsBlocked() function
      2. define the init() function
      3. run init()

      Thanks, Jonathan, this is simple and works well 🙂

  4. Hello. My previous adblock detector stopped working (but works on my local machine). I’m trying different tutorials and they all fail, including this one. Maybe you can help me understand, can’t you? where should I put your code? I put it both after body starts (where my prev. code was) and before body. Doesn’t work equally.
    2) Maybe you can fix my prev. code:
    document.addEventListener(“DOMContentLoaded”, function() {
    setTimeout(function() {
    if ((document.getElementById(“wrapper”).offsetHeight) == 0) {
    document.getElementById(“wrapper”).innerHTML = ‘my HTML here’;
    }, 1000)
    It works locally, but stopped working on my site.
    3) Also I found this code:
    (function() {
    document.body.innerHTML = document.body.innerHTML + ”;
    setTimeout(function() {
    let elm = document.getElementsByClassName(“adblock-detect”)[0];
    if (!elm || !(elm.offsetWidth > 0 || elm.offsetHeight > 0)) {
    document.dispatchEvent(new Event(“adblock-detected”));
    }, 1000);
    document.addEventListener(“adblock-detected”, function() {
    // execute anything if AdBlock is detected
    console.log(‘AdBlock detected’)
    It works, but socilal widgets/plugins (and maybe something else) will not load with this code. Can I put it differently, so that everything will work?

Leave a Reply

Your email address will not be published. Required fields are marked *