Ad Blocker detection AKA Adblock Plus

前端 未结 15 1404
北海茫月
北海茫月 2020-12-02 06:14

After searching Google and Stackoverflow for a few hours I could not find a solution. What I\'m trying to do is detect Adblock plus and display a simple message for now.

15条回答
  •  甜味超标
    2020-12-02 06:41

    The following snippet will pretty much detect all ad blockers. Requires jQuery.

    (function(){
        var bait = 'http://googleads.g.doubleclick.net/pagead/gen_204?id=wfocus&gqid=advertisment&advert=ads';
          $.ajax({ url: bait, dataType: "script"})
          .fail(function () { alert('ad blocked'); })
          .abort(function () { alert('ad blocked'); });
        })();

    It's wrapped in a self-executing anonymous function so it doesn't interfere with other vars or code on the site.

    The bait uses the most popular ad serving network (Google's double click) and includes a few other query params used by easylist and others.

    The fail() and abort() methods are both required, but only one or the other will be invoked.

    Don't put the code in adblocker.js or similar since those sort of filenames themselves get blocked from loading. Either inline it or include it in an random/arbitrary filename or combine it in your main site JS file.

提交回复
热议问题