Search code examples
javascriptadsenseadblock

How can I detect if my AdSense ad is blocked?


If user has some kind of ad blocker installed, ad blocker will of course remove all ads from my website, and it leaves empty spaces where the ads used to be. I would like to use that empty space by putting some other content in it, like links to most important pages of my website, to do that I need to detect if AdSense javascript is loaded.

Methods that I have tried so far:

if (!document.getElementById("google_ads_frame1"))
{
}

and:

if (typeof(window.google_render_ad) == "undefined")
{
}

Both of those seem to fail in certain situation, for example if browser downloads AdSense javascript files a bit slower, it will execute above mentioned code before AdSense code is loaded and I end up hiding ads for users that don't even have ads blocked.

Do you have any suggestions on how could I make sure that my code is run after AdSense? Or some other way of detecting that AdSense scripts are not loaded?


Solution

  • Run this code on the window.onload event. window.onload event is fired when the page has completed loading.

    window.onload = function() {
      // your checks
    }
    

    If you're using jQuery, use

    $(window).load(function() {
      // your checks
    });