我试图在我的网站中实现一些简单的广告拦截器,结果证明没有合并PHP的echo和javascript的document.write。有谁可以帮我这个?
看起来像这样:
echo '
<script type="text/javascript">
if (document.getElementById("tester") != undefined)
{
document.write(\'<form method="post" action="index.php">'.recaptcha_get_html("XXX").'
<br /><input type="submit" value="CLAIM" /></form>\');
}
else
{
document.write(\'<p>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. To read more about why you should disable ABP, please <a href="#">click here</a>.</p>\');
}
</script>';
但是当执行时,我会在提交输入后显示所有内容作为文本。像这样:
答案 0 :(得分:1)
我认为这有助于整理你。关键是退出PHP打印我们的HTML&amp; JS,那么你可以减少报价转义的麻烦。您可以使用表单的单行添加PHP内容
<?php echo $foo ?>
以下是示例......
<?php
// Your code here, eg
function recaptcha_get_html() { return '...' }
// Now, this is temporarily the end of your PHP code, so stop PHP and enter HTML:
?>
<script type="text/javascript">
if (document.getElementById("tester") != undefined)
{
document.write('<form method="post" action="index.php">');
document.write('<?php echo recaptcha_get_html("XXX") ?>');'
document.write('<br /><input type="submit" value="CLAIM" /></form>');
}
else
{
document.write("<p>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. To read more about why you should disable ABP, please <a href='#'>click here</a>.</p>");
}
</script>;
<?php
// Now back into PHP
// Your code carries on here...
我假设
recaptcha_get_html("XXX")
是PHP。如果它是一个JS函数,你需要做一些稍微不同的事情。
警告!以上是未经测试的 - 它旨在向您展示一种风格,它可以帮助您整理代码以使其工作,而不是确定的正确答案 - 但我希望它有所帮助: - )
答案 1 :(得分:0)
你在做什么是一种非常糟糕的做法。也许你可以在你的html里面尝试下面的
<script type="text/javascript">
var recaptcha = "<?php echo recaptcha_get_html("XXX"); ?>";
if (document.getElementById("tester") != undefined)
{
document.write('<form method="post" action="index.php">'+recaptcha+'
<br /><input type="submit" value="CLAIM" /></form>');
}
else
{
document.write('<p>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. To read more about why you should disable ABP, please <a href="#">click here</a>.</p>');
}
</script>
但是,如果您真的想使用它,请在下面找到我认为问题的原因
document.write(\&#39;
我们检测到您正在使用 AdBlock Plus 或其他一些广告拦截软件请注意,这只会导致网站的消亡。我们需要资金来运营网站,而且几乎所有这些都来自我们的在线广告。要了解更多有关禁用ABP的原因,请点击此处。< / p> \&#39);
将被错误地解释为因为这里有4个单引号,你会感到困惑。作为document.write的输入提供的字符串应该只具有double或single的引用类型,因此您可以通过两种方式编写上述代码
document.write(\'<p>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. To read more about why you should disable ABP, please <a href="#">click here</a>.</p>\');
document.write(\"<p>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. To read more about why you should disable ABP, please <a href=\\\"#\\\">click here</a>.</p>\");
document.write(&#39;&#39;)中的引号很重要;应该是双重转义,因为我们举一个例子:
你想打印html =&gt; We've a nice dog
所以从javascript应该是=&gt; document.write('we\'ve a nice');
所以你要得到\&#39;在php里面的document.write应该是=&gt;
echo 'document.write(\'we\\\'ve a nice\');';
因为php中的\\\'
将成为\ {#1}}在javascript中将会变成&#39;在HTML中