How to detect Adblock on my website?

2019-01-01 07:23发布

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?

45条回答
心情的温度
2楼-- · 2019-01-01 08:02

My advice is: don't do it!

Any scenario where you treat people as "wrongdoers" is going to result in them fighting back.

Here's my proposal.

Put a small unobtrusive message at the top of the page (regardless of whether ads are being blocked) with the text I *totally* respect your right to block ads and a link to another page/pop-up entitled Read more ....

On the other page, make it clear that you understand it's their computer and they are free to use ad blocking.

Also make it clear in a non-accusatory way that the use of these blockers makes it more difficult for you to deliver great content (explaining why in detail) and that, while you'd prefer the ad blocking to not happen on your site, it's totally their decision. Focus on the positives of turning off blocking.

Those who are vehemently opposed to ads will ignore this but you never stood a chance of convincing them anyway. Those who are indifferent may well be swayed by your appeal since you're not doing the whole "let me get my way or I'll take my ball and go home" thing that honestly should be the exclusive domain of five year old children.

Remember, no-one held a gun to your head and forced you to put your stuff on the net. Treat your readership/users with respect and you'll probably find a good number of them will reciprocate.

查看更多
与君花间醉酒
3楼-- · 2019-01-01 08:03

An efficient way to check if there is an adblock: Simply check if there is adblock enabled by trying to trigger the URL of google ads. If yes then run the callback_has_adblock, if not then run the callback_no_adblock. This solution costs one request more but at least it works:

var hasAdBlock = function (callback_has_adblock, callback_no_adblock) {

    $.getScript( "http://pagead2.googlesyndication.com/pagead/show_ads.js" )
        .done(function( script, textStatus ) {
            callback_no_adblock();
        })
        .fail(function( jqxhr, settings, exception ) {
            callback_has_adblock();
    });
};

This solution works for all kind of ads, not only google adsense.

查看更多
冷夜・残月
4楼-- · 2019-01-01 08:03
旧时光的记忆
5楼-- · 2019-01-01 08:04

My easiest solution with jquery is:

$.ajax({
    url: "/scripts/advertisement.js", // this is just an empty js file
    dataType: "script"
}).fail(function () {
    // redirect or display message here
});

advertisement.js just contains nothing. When somebody uses adblock, it fails and the function gets called.

查看更多
高级女魔头
6楼-- · 2019-01-01 08:05

No extra requests. No external libraries. Just plain, simple JavaScript:

var adBlockEnabled = false;
var testAd = document.createElement('div');
testAd.innerHTML = ' ';
testAd.className = 'adsbox';
document.body.appendChild(testAd);
window.setTimeout(function() {
  if (testAd.offsetHeight === 0) {
    adBlockEnabled = true;
  }
  testAd.remove();
  console.log('AdBlock Enabled? ', adBlockEnabled)
}, 100);

  • You create an element with the class adsbox (as defined as a removable element in the definition file of AdBlock Plus)
  • You add it to the document and after a short while you read its offsetHeight
  • If AdBlock is installed, the element won’t have any height.

Credit to Christian Heilmann's post, I think it's by far the best solution for detecting AdBlock.

查看更多
孤独寂梦人
7楼-- · 2019-01-01 08:05
登录 后发表回答