Hannes
Hannes

Reputation: 8237

Adblock is blocking iframe redirects - how to detect?

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.

Upvotes: 4

Views: 10309

Answers (2)

Liam Dawson
Liam Dawson

Reputation: 1199

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.

Upvotes: 0

Linus Kleen
Linus Kleen

Reputation: 34662

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").

Upvotes: 2

Related Questions