Pregunta

This is my situation; I am displaying ads on my website but I want to display a specific banner if a visitor is using an ad blocker. First thing I've looked for is a script that detects the ad blocker, but after trying a few different scripts it seems most of them no longer work (at least, I couldn't get them to work).

So I gave up on that and went with a different solution. Displaying a CSS background image behind the ad so that if the ad isn't shown, the image is. Because a typical ad takes a moment to load I made the background image a GIF image with 2 seconds of transparency. This works like a charm the first time, but when you reload the page or open a different page the GIF animation doesn't play and instantly displays the last frame, skipping the transparency.

I've tried adding random stuff behind the URL in the CSS, which didn't work. I've tried a data/inline version of the image, that didn't seem to work either. I'm kinda running out of solutions.

The CSS:

.ads {
position: relative;
top: 15px;
float: right;
height: 60px;
width: 468px;
background-image:  url('/images/ads/ads_top.gif?randomstuff=39485')
}

I'm basically looking for either;

1) A way to show an alternative image if the ad is blocked (that is still actual and works).

2) A way to delay a CSS background image from being loaded.

3) A way to prevent a GIF from being cached or forced to replay the animation on each pageload.

Any of these would fix my problem. Hope someone is able to help.

Thanks!

¿Fue útil?

Solución

Look this link. It is very simple and I don't think you need comments. Another question is how to set up time to each image.

Time to use some jQuery:

Your html code:

<div class='ads'></div>
<div class='ads'></div>

And the css code:

.ads {
position: relative;
top: 15px;
float: right;
height: 60px;
width: 468px;
}

Your jQuery code:

$(".ads").each(function() {
    var timestamp = $.now();
    $(this).css("background-image", "url('/images/ads/ads_top.gif?"+timestamp+"')");
});

Your jQuery code have to be placed into the .js file. Do you have some js files? If yes, then add my code into onload handler. If you don't have any create new file, say, scripts.js and put this code into it:

$(document).ready(function()
{
    $(".ads").each(function() {
        var timestamp = $.now();
        $(this).css("background-image", "url('/images/ads/ads_top.gif?"+timestamp+"')");
    }); 
}

Explanation:

  1. .ready function means that all instructions in body of this function will be read and started on page load. You don't need them to work before page loaded, right?
  2. $(".ads") — we get element with selector .ads (with class ads).
  3. $(".ads").each(function() { /* body */ }.each function means that we will assign instructions from function body to all elements with selector .ads
  4. var timestamp = $.now(); — getting timestamp and assigning it into variable
  5. $(this).css("background-image", "url('/images/ads/ads_top.gif?"+timestamp+"')"); — adding css property to $(this) element (this element is current element with selector .ads)

Thats all. Simple. Now you have file scripts.js with content above. Put it somewhere on your site, where you usually put your media files. For example, {root}/media/ <-- here.

The last thing you should do is link your new js file and jQuery library. Note, that jQuery library have to be linked before file, using $ variable.

Add next code to the <head></head> tag to your view:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="/media/script.js"></script>

Don't forget to do all js actions in onload handler.

Hope this will help. Tell me about result, please.

JSFIDDLE

Otros consejos

A possible solution for option 1, is to check with javascript (or preferably jquery) if the banner is visible. (You probably need to put a setTimeout around it, because as far as I know the page js loads first, and after that the adblocker js.

var ads = $('.ads').filter(':visible');
if(!ads.length) {
    //do your alternative image showing magic here
}

A possible solution for option 2 might be to link to a php script and put a sleep(2); inside it, with after that the appropriate headers and print/echo of the image.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top