2

I've been trying to incorporate an Adblock detecter that will display one image when Adblock is detected and another when Adblock isn't detected. The only problem is that the image is displayed and formatted by CSS. So is there a way to change the image that CSS displays through js?

html:

<div id="adblockFrame">
<script type="text/javascript" src="js/advertisement.js"></script>
<script type="text/javascript">
    if (document.getElementById("TestAdBlock") != undefined)
    {
        document.getElementById('adblockFrame').innerHTML="<img src='images/ImageHolderno.png'      alt='no adblock' />";
    }
    else
    {
        document.getElementById('adblockFrame').innerHTML="<img src='ImageHolderyes.png' alt='Adblock detected!' />";
    }

Bear in mind that the image place holders and void and do not serve a purpose as the image is defined by CSS.

CSS:

#mainPicture .picture
{
position:relative;
width:650px;
height:325px;
top:10px;
background-image:url(../images/minibg1.png);
background-repeat:no-repeat;
margin-left:10px;
}
2
  • Add a class to the element and act on that in the CSS. Commented Jan 14, 2014 at 23:25
  • You could set up two similar CSS classes and switch it with javascript instead of trying to alter a property of a single CSS class. Commented Jan 14, 2014 at 23:26

1 Answer 1

1

JS:

document.getElementById('adblockFrame').className = 'adblockersSuck';

CSS:

.adblockersSuck {
    background:url(ImageHolderyes.png) no-repeat;
}

Or insert it dynamically with pseudo-elements:

.adblockersSuck:after {
    content:url(ImageHolderyes.png);
}
Sign up to request clarification or add additional context in comments.

4 Comments

How would I incorporate the third section 'pseudo-elements'
Ok i've got the two images to show based on the output of the js. The only problem is that when Adblock is enabled the image shows behind the Adblock is disabled image and isn't in the right place.
You can put any style you want on the pseudoelement - including position:relative, z-index:1 and top:-50px.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.