4

I have page that performs a lot of redirects inside an iframe where the targets are mostly affiliate network pages (that perform redirects to shops and so forth), the markup looks something like:

http://jsfiddle.net/HPDNC/2/

As you can see, if you have an ad blockerk enabled the iframe doesn't load. I need to somehow detect that so I can ether make a direct redirect or at least inform the user of the situation.

The normal way would be to simply check the ad or what not to detect if an ad blocker is active. Unfortunately, there are no actual ads on this page to check.

Charles
  • 50,943
  • 13
  • 104
  • 142
Hannes
  • 8,147
  • 4
  • 33
  • 51

2 Answers2

2

You can detect whether or not a site of yours is visited with Ad-Blockers. In the <head> tag, - or really anywhere - put this:

<script type="text/javascript">
window.ADS_BLOCKED = true;
</script>
<script type="text/javascript" src="/advertise/detect.js"></script>
<script type="text/javascript">
if (window.ADS_BLOCKED)
   alert('You blocked me...');
</script>

The included Javascript detect.js would set window.ADS_BLOCKED to false. Ad-Blockers would prevent this file from loading because of its filename ("advertise").

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
  • 1
    You're relying on the ad-blocker working on a very crude scale. Some systems (e.g. Peerblock) have lists of known advertising sites, and block the sites directly. Other systems work off element names. You can't be guaranteed every system will work this way. Some will, but not all. – Liam Dawson Feb 07 '12 at 11:10
  • something like that has already come to my mind but because of the reasons @dawmail333 mentioned it would be my last resort – Hannes Feb 07 '12 at 11:26
0

Give the iframe an id, then you can check for the existence of the iframe using javascript.

Here's an example:

<script type="text/javascript">

if(document.getElementById("ad") == null) {
    alert("The ad has been removed!");
}
else
{
    alert("It's alright, it's still here.");
}

</script>

EDIT: Just fixed an error.

What this does is gives Javascript a means of accessing your ad element. The Javascript code that comes after (if(document.getElementById("ad") == null)) just checks if the element exists - if it doesn't, it means the adblocker has removed it.

Some adblockers (like the earlier versions of AdBlock for Chrome) just hide the element, instead of removing it - I'll leave that as an exercise for you to do, because I've only ever checked CSS on DOM elements through JQuery.

EDIT 2:

Using this answer here, you could simply check if the HTML in the iframe loaded properly, and respond based on that.

Community
  • 1
  • 1
Liam Dawson
  • 1,189
  • 13
  • 27
  • addblock does not destroy the iframe - but just stops the request inside the iframe so it would exist as an element inside the parent document whats so ever – Hannes Feb 07 '12 at 11:24
  • Ok, I'll see if I can find a solution for that later then. – Liam Dawson Feb 07 '12 at 11:54