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 }