0

I have a setup to display something when it detects adblock, and it's displaying code instead of working. I am not that experienced in Javascript to know why. Okay, in my index file I have

<script type="text/javascript" src="inls/advertisement.js"></script>

Now in advertisement.js I have

document.write('<div id="tester">an advertisement</div>');

And now I have the following in my index after that:

<script type="text/javascript">
if (document.getElementById("tester") != undefined)
{ 
document.write('<center><script type="text/javascript"><!--google_ad_client = "ca-pub-3846434391631935";/* talknow */google_ad_slot = "6591659297";google_ad_width = 728; google_ad_height = 90; //--> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></center>');
 } else {
    document.write('<p class="no">We\'ve detected that you\'re using <strong>AdBlock Plus</strong> or some other adblocking software. Please be aware that this is only contributing to the demise of the site. We need money to operate the site, and almost all of that comes from our online advertising. Its not nice to steal.<!-- end .content --></p>');
}
</script>

but where that code is, it displays as this:

enter image description here

Can anyone help?

Austen
  • 275
  • 1
  • 4
  • 16
  • I'd suggest some different wording for your msg: "We've detected AdBlock Plus or some other adblocking software. The ability to provide this site comes from small amounts of revenue from displaying ads. If you don't allow those ads to be displayed, then this site may not be able to continue to operate. Please support this site by either allowing the ads to be displayed or making an appropriate donation. – jfriend00 Jul 07 '13 at 22:40
  • @nnnnnn I know but like I said, I'm not that experienced in Javascript so I didn't know if ' would break the code. I'm only trying to implement a script to suit my needs, I didn't code this myself. – Austen Jul 07 '13 at 22:42
  • You just have to escape the apostrophe with a backslash. You've already done so earlier in the same string: `'We\'ve...you\'re...'`. So just do the same thing: `'...It\'s not...'` – nnnnnn Jul 07 '13 at 22:50

1 Answers1

0

I took a look at your actual website's source code. It's different to what you posted:

<script type="text/rocketscript">
if (document.getElementById("tester") != undefined)
{ 
document.write('<center><script type="text/javascript"><!--google_ad_client = "ca-pub-3846434391631935";/* talknow */google_ad_slot = "6591659297";google_ad_width = 728; google_ad_height = 90; //--> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></center>'); } else {
    document.write('<p class="no">We\'ve detected that you\'re using <strong>AdBlock Plus</strong> or some other adblocking software. Please be aware that this is only contributing to the demise of the site. We need money to operate the site, and almost all of that comes from our online advertising. Its not nice to steal.<!-- end .content --></p>'); }
</script>

Based on Why is wordpress placing "text/rocketscript" instead of "text/javascript" when using wp_register_script()? it seems like you need to change your <script> tag to:

<script data-cfasync="false" type="text/javascript>

Also, document.write is considered "bad practice"; you should probably use DOM manipulation instead, i.e. something like:

var adblock = document.createElement('p');
adblock.className = 'no';
adblock.innerHTML = 'Some text here';

var content = document.getElementById('content');
content.insertBefore(adblock, content.firstChild);
Community
  • 1
  • 1
Danny Beckett
  • 18,294
  • 21
  • 100
  • 129