JavaScript code error: image doesn’t show

I am working on a WordPress site and I have placed the following code in my JavaScript file:

function blockAdblockUser() {
    if ($('.sideAd').height() == 0) {
        "<a href='http://google.com' target='_blank'><img src='image.png' /></a>";
    }
}

$(document).ready(function(){
    blockAdblockUser();
});

…and I put the following code in my html…

Read More
<section class="sideAd">
    <!-- google ad script here -->
</section>

Basically, I am trying to get the alternate image to show up if the user is blocking ads with AdBlock. However, with AdBlock turned on, the image doesn’t show. I am thinking there is something wrong with how I wrote this line "<a href='http://google.com' target='_blank'><img src='image.png' /></a>"; I am still a novice when it comes to JavaScript.

Can anybody see where the problem is?

Related posts

Leave a Reply

3 comments

  1. You need to take the string that you’ve made and actually set it as the inner html. You can fix it by changing the inner line to:

    $('.sideAd').html("<a href='http://google.com' target='_blank'><img src='image.png' /></a>");
    
  2. try:

    function blockAdblockUser() {
        if ($('.sideAd').html() == "") {
            $('.sideAd').append("<a href='http://google.com' target='_blank'><img src='image.png' /></a>");
        }
    }
    
  3. function blockAdblockUser() {
        if ($('.sideAd').contents().length) {
            $('.sideAd').append("<a href='http://google.com' target='_blank'><img src='image.png' /></a>");
        }
    }