Search code examples
javascriptjquerycachingadblock

Detect ad block script, runs properly only on hard reload


I use this script FuckAdBlock to detect ad block to my website. In summary when a page loads it checks if ad block is enabled. If it is, a function is triggered in which you can notify user etc.

It works fine but there is an issue. It runs only when I hard reload the page. On normal reload nothing happens and that's the issue.

        // Function called if AdBlock is not detected
        function AdBlockNotDetected() {
            alert('AdBlock is not enabled');
        }
        //Function called if AdBlockis detected
        function AdBlockDetected() {
            alert('AdBlock Detected');
        }


        if(typeof fuckAdBlock === 'undefined') {
            AdBlockDetected();
        } else {
            fuckAdBlock.onDetected(AdBlockDetected);
            fuckAdBlock.onNotDetected(AdBlockNotDetected);
        }

Solution

  • Try running the check function on page load like so:

    $(document).ready(function(){ 
       // Manually check if AdBlock is enabled.
        fuckAdBlock.check(loop);
    }) 
    

    See: https://github.com/sitexw/FuckAdBlock#method-available