0

我有一个设置,当它检测到 adblock 时显示一些东西,它显示代码而不是工作。我在 Javascript 方面没有那么丰富的经验,不知道为什么。好的,在我的索引文件中我有

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

现在在 ads.js 我有

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

现在我的索引中有以下内容:

<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>

但是该代码在哪里,它显示为:

在此处输入图像描述

任何人都可以帮忙吗?

4

1 回答 1

0

我查看了您实际网站的源代码。它与您发布的内容不同:

<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>

基于为什么在使用 wp_register_script() 时 wordpress 放置“text/rocketscript”而不是“text/javascript”?似乎您需要将<script>标签更改为:

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

此外,document.write被认为是“不好的做法”;您可能应该改用 DOM 操作,例如:

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

var content = document.getElementById('content');
content.insertBefore(adblock, content.firstChild);
于 2013-07-07T22:39:24.857 回答