I would like to be able to detect if the user is using adblocking software when they visit my website. If they are using it, I want to display a message asking them to turn it off in order to support the project, like this website does.
If you enter to that site and your browser has some kind of adblock software enabled, then the site instead of showing the actual ads shows a little banner telling the users that the ad revenue is used for hosting the project and they should consider turning Adblock off.
I want to do that on my website, I'm using adsense ads on it, How can I do that?
I understand your tension and you can check if element has been created by script or element is hidden. And if we speak about ad-blocking you can count only on the element visibility, not on the element presence.
Element created with third-party script will never be present, that if script is not reachable at the moment (DNS error, remote web server error, offline web page preload, etc), and you'll always get false positive.
All other answers with checks are correct, but keep this in mind.
My solution is not specific to a certain ad network and is very lightweight. I've been running it in production for a few years. AdBlock blocks all URLs containing the word "ads". So this is what I did:
I added a small js file to my webroot with the name
ads.js
This is the only line of code in that file
Then somewhere in my page:
Files like ads.js are blocked by at least these adblockers on Chrome:
(Yes, these are completely different browser extensions)
This does not work with:
Most ads are dynamically loaded in javascript. I just used the onerror event to detect whether the ad script could be loaded or not. Seems to work.
Example with GoogleAds:
This can be used on other elements as well to see if an ad blocker is blocking the content. This method can produce false positives if the remote elements doesn't exist or cannot be reached.
You don't need an extra HTTP request , you may simply calculate the height of a fake add.
By the way, here is a full list matching the elements that adblockers avoid rendering.
If using the new AdSense code, you can do an easy check, with out resorting to content or css checks.
Place your ads as normal in your markup:
Then you call the adsense code at the bottom of your page (note do not use the
"async"
flag when calling theadsbygoogle.js
script):Then add this little snippit of code below that:
AdSense always creates/sets the flag
adsbygoogle.loaded
totrue
when the ads are loaded, You could place the check in a setTimeout function to delay the check by a few seconds.To detect if the user is blocking ads, all you have to do is find a function in the ad javascript and try testing for it. It doesn't matter what method they're using to block the ad. Here's what it looks like for Google Adsense ads:
This method is outlined here: http://www.metamorphosite.com/detect-web-popup-blocker-software-adblock-spam