How to Detect Ad-Blocker on your Website
This article is for blogger like me who support their passion for blogging through running Ad sense or any other ads on their blog. We know(even use) Ad blockers keep annoying pop up ads away.But it also blocks the Ad-sense ads(or no so annoying ads).
This is the reason why bloggers get less ad-impressions than the visitors on their blog/site.So in this post I will show you how to detect ad blocker on your site using JavaScript(and earn more :) ). This will be also useful for those who are learning web development.
What is an AdBlocker(and how to deal with them)?
The Ad Blocker is usually a browser plugin that disables the ads in the browser.Adblocker are really popular now-a-days and almost everyone uses it. Most of the ad blockers if not all of them lets you choose where(which site) to block ads and where to not. Hence we can force our users to whitelist our site(i.e. turn off ad blocker for our site). That is what we are going to do here.
var banner = document.getElementById("banner");
window.onload(function()
{
if(banner.offsetHeight > 0)
{
alert('No AdBlock :)');
}
else
{
alert('AdBlock Detected ');
}
});
Here is an example.,
Declare any global variable in it.
Block The Adblocker
We will use java script to do it. The idea behind this is that ad-blocker blocks anything that looks like an ad on your website. So we will trick the Adblocker by making a fake on our site. Then we will check our that if our fake ad is blocked or not. If it is blocked then we are going to show a message to our visitor to whitelist our site.
[If you don't know JavaScript then here is a short&sweet JS tutorial for you.]
Lets create an Ad...
First create HTML div for our ad banner,
<div class="adBanner" id="banner">
</div>
then style it using css,
.adBanner
{
background-color: transparent;
height: 100px;
width: 100px;
}
now we will write our javascript
var banner = document.getElementById("banner");
window.onload(function()
{
if(banner.offsetHeight > 0)
{
alert('No AdBlock :)');
}
else
{
alert('AdBlock Detected ');
}
});
As you might have guessed the ad banner will not render on our site if ad block is on, hence our visitor will get a message to turn it off.
Of course you can style it, make it look more professional than just a simple alert message but this is the basics.Remember, trick lies in making our <div> tag as much suspicious for adblocker as possible so it blocks our site.
I have checked it and it works with most of the adblockers, but remember it might not work with some of them(like ghostery). For them, you can try loading an external JavaScript from your webpage and name it like ads.js or something similar so that it get blocked by that ad-blocker.
Here is an example.,
Declare any global variable in it.
//Write this in ads.js file.
var isloaded;
now go to your web page check if it is declared by
//write/copy it in your script tag on your webpage,
if (typeof isloaded == "undefined") //isloaded will be not available if ads.js is blocked
{
{
alert("Please Disable your Adblcoker");
}
Hope you will find it useful.If you have any questions you can ask in comments.
See You Soon!!
Comments
Post a Comment