[email protected]
Twitter
LinkedIn
GitHub
  • Blog
  • GitHub
  • Resume

Detecting AdBlock with jQuery or JavaScript

August 13, 2015Front-End, TricksNo Commentsjfadev

Recently a large number of users use browser extensions that block advertising, the most known is AdBlock Plus. If our website is financed thanks to the publicity this can be a problem. In this case we can use JavaScript to detect if AdBlock or another similar plugin is blocking our advertising.

A cunning may be placing the script of advertising within a div and check if your height is greater to 0.

Html:

<div class="myAd">
    <!-- Ad code -->
</div>

JQuery:

jQuery(document).ready(function() {
    if (jQuery('.myAd').height() == 0) {
        // AdBlock active
    }
});

Js:

var interval = setInterval(function() {
    if (document.readyState === 'complete') {
        clearInterval(interval);
        if (document.getElementsByClassName('myAd')[0].clientHeight > 0) {
            // AdBlock active
        }
    }    
}, 100);

Another cunning can be check for iframes since ads like AdSense use them.

jQuery:

if (jQuery('iframe').length > 0) {
    // AdBlock active
}

Js:

if (document.getElementsByTagName("iframe").item(0) == null) {
    // AdBlock active
}

: AdBlock, Ads, jQuery, JS, Web Developement
jfadev
https://jordifernandes.com
Programmer and Web Developer since 2006. Graduate in Software Engineering in Marseille 15+ years of experience in Web Technologies.

Twitter

My Tweets

Rencent Posts

  • Display a specific Instagram post in WordPress with Jfa Social Media Post
  • Build a custom image and distribute them to Azure VMSS using GitHub Actions
  • PHP-FPM Process Calculator
  • Jfa PWA Toolkit
  • List of backlinks for SEO
  • Full list backlinks generators for SEO positioning
  • Modal Dialog for Material Design Lite
  • Run Symfony2 command from cronjobs in CPanel on a shared host
  • Detecting AdBlock with jQuery or JavaScript
  • Video: Introduction to Apache Cordova in Spanish

Categories

  • Back-End (5)
  • DevOps (1)
  • Front-End (12)
  • Monetization (0)
  • News (8)
  • SEO (2)
  • Tools (6)
  • Tricks (10)
  • Uncategorized (3)

Discover

  • Los Mejores Top 10
  • Slow Fashion Shop
Twitter
LinkedIn
GitHub

© 2013-2022 Jordi Fernandes Alves (@jfadev)