Skip to content

Instantly share code, notes, and snippets.

@ricealexander
Created November 19, 2018 18:44
Show Gist options
  • Save ricealexander/2724d6cf39a36c52309f82f68491674e to your computer and use it in GitHub Desktop.
Save ricealexander/2724d6cf39a36c52309f82f68491674e to your computer and use it in GitHub Desktop.
detectAdblock() function
const detectAdblock = ()=> {
const adSample = '<div class="adBanner">&nbsp;</div>';
document.body.insertAdjacentHTML('beforeend', adSample);
const bannerHeight = document.querySelector('.adBanner').offsetHeight;
document.querySelector('.adBanner').remove();
return (bannerHeight === 0) ? true : false;
}
console.log(detectAdblock());
// Adblock Detection
/// This code detects if an adblocker is being used
/// https://html-online.com/articles/detect-adblock-javascript/
/// I should preface that I'm a huge proponent of AdBlockers
/// As they can block annoying popups and malicious links,
/// I'd reccomend them to anyone,
/// with the suggestion that they whitelist friendly sites.
/// The article that inspired this code built a solution
/// that uses HTML, CSS, and JavaScript.
/// I felt that a solution that checks for an adBlocker
/// should be a pure-javascript solution.
/// This code works by inserting an element with the
/// .adBanner class, which adBlockers are known to block.
/// It then determines whether the element is visible or not,
/// and then removes the element, to prevent clutter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment