Skip to content

Instantly share code, notes, and snippets.

@irazasyed
Created July 21, 2014 16:47
Show Gist options
  • Select an option

  • Save irazasyed/3d247d3d121e781a3872 to your computer and use it in GitHub Desktop.

Select an option

Save irazasyed/3d247d3d121e781a3872 to your computer and use it in GitHub Desktop.
AdBlocker Detecting Script - Mirror

Method 1: You can check for the existence of window.google_jobrunner after the page has finished loading. We are using setTimeout to take care of asynchronous Google AdSense that may not load immediately.

<script>
  window.onload = function() {
    setTimeout(function() {
      if ( typeof(window.google_jobrunner) === "undefined" ) {
        console.log("ad blocker installed");
      } else {
        console.log("no ad blocking found.");
      }
    }, 10000);  
  };
</script>

Method 2: The other more popular approach is that you create a file called /ads.js in your server and inside that file, set a variable as false. AdBlockers routinely block JavaScript files that have .ads in the name and hence, the variable will not be set if the ad blocker is active.

// Put this in the ads.js file
isAdBlockActive=false;

Now put this somewhere inside the HTML of your main web page.

<script>var isAdBlockActive=true;</script>
<script src="ads.js"></script>
<script>
if (isAdBlockActive) { 
  console.log("The visitor is blocking ads");
}
</script>

Method 3: Here’s another option that works with the new Asynchronous Responsive Google Ads.

<script>
window.onload = function() {
  setTimeout(function() {
    var ad = document.querySelector("ins.adsbygoogle");
    if (ad && ad.innerHTML.replace(/\s/g, "").length == 0) {
      ad.style.cssText = 'display:block !important';
      ad.innerHTML = "You seem to blocking Google AdSense ads in your browser.";
    }
  }, 2000); 
};
</script>

In the new format, the ads are inserted using the INS tag. The snippet checks for the length of the tags that are contained inside the INS tag. If it is 0, Google Ads were blocked and the user is shown a custom message.

We also need to set the CSS display property as block as AdBlock may be blocking ads with the “adsbygoogle” class by simply hiding them on the screen with CSS.

Source


I don't take credits nor hold copyrights for the above mentioned scripts. Added here just so i can remember easily and access whenever i want, all rights/credits go to the respective copyright holder of the article.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment