last updated : Apr 13 2016

anti-contentBlocker.js :

If you place Google AdSense code on your web page, this script will work well.

How to detect Ad / content Blocker?

If you place Google AdSense code on your web page, there is a very simple way to ditect Blockers with simple JavaScript code.

An AdSense code will append IFRAME elements to its INS element ( that has 'adsbygoogle' class attribute ) when it has been executed successfully.

So you can detect user's AdBlocker enabled ( or not ) to count the number of IFRAME elements in <ins class="adsbygoogle"> element, when the web page has been fully loaded.

That is :

  • If you found at least one IFRAME element, it means an AdSense Code has been successfully executed, and the visitor would NOT use AdBlocker.
  • If you could not found IFRAMEs, the visitor would use AdBlocker.


Here is an example code of Google AdSense.

<script async src="//"></script>
<!-- adsense sample -->
<ins class="adsbygoogle"
(adsbygoogle = window.adsbygoogle || []).push({});

And here is an example of JavaScript code to count IFRAME elements in the elements that have an class name of "adsbygoogle".

function detectBlocker( ){
	var ad = document.getElementsByClassName( 'adsbygoogle' );
	if( ad.length == 0 )return;  /* No AdSense code in this web page. */
	if( ad[0].getElementsByTagName('IFRAME').length == 0 ){
		/* No IFRAME found. AdSense code has not been executed. */
		/* Do Something for AdBlocked user. */

And attach this function to window.onload event.

window.addEventListener('load', detectBlocker, false);

That's all.

But you should write a function doSomething() which does action against AdBlocked users. See below, an sample script is available.


This is the sample script of anti-AdBlocking.


Installation guide is here . Anti-contentBlocker is enabled in the landing page. Turn on your browser's AdBlock Extension and visit Installation guide page, you can see anti-contentBlocker.js in action.

This script displays the following message when AdBlocker was detected.

If the browser language is set to Japanese (ja or ja-JP), the message will be displayed in Japanese. Except Japanese, in English.

For more detail, see here.