DEV Community

sagarjaid
sagarjaid

Posted on

Detect any AdBlocker with Javascript code.

// This is react example.

import { useEffect } from 'react';

const hello = () => {
    useEffect(() => {
        const timeoutId = setTimeout(() => {
            const adBoxEl = document.querySelector(".ad-box");
            const hasAdBlock = window.getComputedStyle(adBoxEl)?.display === "none";
            console.log(hasAdBlock, "hasAdBlock");
            // ... save to DB
        }, 1000);
        return () => clearTimeout(timeoutId);
    }, []);

    return (
        <div className="ad-box" style={{ position: "fixed", top: 0, left: 0 }} aria-hidden="true">
            &nbsp;
        </div>
    );
}

export default hello
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
tavsbekons profile image
Astoņi Kustoņi

So basically aadblock has to make a dynamic class to prevent this to work. Funk them ads.

Collapse
 
sagarjaid profile image
sagarjaid

hope this helps :)