How to Add a Message to Detect Adblock with JavaScript

How to Add a Message to Detect Adblock with JavaScript
Vikelsik
Vikelsik •
Table of Contents
If you want to know whether visitors to your blog are blocking Google AdSense ads or not, you can do so by adding JavaScript code.

The JavaScript code I'm about to share works by automatically detecting the Adblock extension if visitors have Adblock enabled in their browser when they open your blog, displaying a special message. This JavaScript code supports the new format of Google AdSense Responsive Asynchronous ad data and uses the ins.adsbygoogle selector, replacing it with a message when Adblock is active in the browser.

This code is also quite lightweight, so you don't need to worry about it slowing down your blog's loading speed. For those who want to try it, please follow these steps:

How to Add a Message to Detect Adblock with JavaScript

1. Log in to Blogger > Open the Template Editor > Then add the code below before the tag </body>

<script>
//<![CDATA[
// Adblock Detected
window.onload=function(){setTimeout(function(){var e=document.querySelector("ins.adsbygoogle");e&&0==e.innerHTML.replace(/\s/g,"").length&&(e.style.cssText="color:rgba(0,0,0,0.5);font-weight:700;font-size:110%;padding:20px;margin:auto;text-align:center;text-decoration:none;display:block!important;background:rgba(0,0,0,0.02);border:1px solid rgba(0,0,0,0.08)",e.innerHTML="Tampaknya Anda memblokir iklan Google AdSense di browser Anda")},2e3)};
//]]>
</script>

Example of message display:

Description:
e.style.cssText="color:rgba(0,0,0,0.5);font-weight:700;font-size:110%;padding:20px;margin:auto;text-align:center;text-decoration:none;display:block!important;background:rgba(0,0,0,0.02);border:1px solid rgba(0,0,0,0.08)"

The marked code is the CSS for the message display. Please modify it as you like.
e.innerHTML="Tampaknya Anda memblokir iklan Google AdSense di browser Anda"

Replace the code marked with the word “friend.”
2. Once added, save the template, then check your blog and activate Adblock to ensure the code is working properly.
That concludes the tutorial on How to Add a Message to Detect Adblock with JavaScript. We hope you find it useful.
Vikelsik
Vikelsik Just to share tricks & tips + interesting information that is useful for the whole world and outer space. Enjoy!

Post a Comment