-3

I've been looking at the tutorial here:

https://www.detectadblock.com/

This is the Javascript they use to detect is Adblock is enabled:

if(document.getElementById('APvpIfBTxbcQ')){
  alert('Blocking Ads: No');
} else {
  alert('Blocking Ads: Yes');
}

I don't have any code that needs to be run if someone is not blocking ads.

So, this line is basically useless to me:

alert('Blocking Ads: No');

What would I need to change to remove that part and basically do this?

if(document.getElementById('APvpIfBTxbcQ') IS NOT SET){
  alert('Ads are being blocked.');
}
Stacker
  • 97
  • 6

2 Answers2

1

If you are using a latest browser, you can do something like this:

console.log("Ads blocked: " + (document.querySelector("#ad-id") === null));
console.log("Ads blocked: " + (document.querySelector("#ad-id-2") === null));
<div id="ad-id">Hi</div>

In the above code, the ad-id-2 is not present. So you can essentially compare it with null.

To be completely sure, you may want to check if the ad is really displaying. You can do using How to tell if a DOM element is visible in the current viewport?

In your original code, you can also use !! to make sure.

if (!!document.getElementById('APvpIfBTxbcQ')) {
  alert('Blocking Ads: No');
} else {
  alert('Blocking Ads: Yes');
}
Praveen Kumar Purushothaman
  • 154,660
  • 22
  • 177
  • 226
1

If you want to test if the opposite of something is true you just put the not (!) symbol in the front of it.

if(!document.getElementById('APvpIfBTxbcQ')){
  alert('Ads are being blocked.');
}