Javascript PHP document.write

时间:2011-12-28 22:26:36

标签: php javascript

  

可能重复:
  Where is the .JS file in this code? And why are they calling it this way?

这是我的JavaScript代码。的 showcode.php

<script type="text/javascript" charset="utf-8">(function(){
var a="publisher-121";
var n="top";
var z="45";
var h="125";
var w="125";
var nofollow="no";
var e=document.getElementsByTagName("script")[0];
var d=document.createElement("script");
d.src=('https:'==document.location.protocol ? 'https://' : 'http://') +"server.com/adcode.php?a="+a+"&h="+h+"&w="+w+"&n="+n+"&z="+z;
d.async = true;
d.defer = true;
d.type="text/javascript";
e.parentNode.insertBefore(d,e);
})();</script>

如您所见,网址指向http://server.com/adcode.php?a=publisher-121&n=top&z=45&w=125&h=125

此页面包含以下内容:( adcode.php

 <?php  echo "document.write('<div id=\"ha_adblock ".$a."_".$z."\" class=\"ha_adblock".$a."_".$z."\" style=\"width:".$h."px;height:".$w."px;background:#eee;text-align:center;cursor:pointer\" onClick=\"window.location.href=\'click.php?a=".$a."&z=".$z."&g=http://refererurl.com/;\'return false;\"><a href=\"http://refererurl.com/\" onClick=\"window.location.href=\'click.php?a=".$a."&z=".$z."&g=http://refererurl.com/;\'return false;\" style=\"color:#333;font-weight:bold\">advert</a></div>');"; ?>

然而 - 页面只显示为空白。 Firebug显示我的代码已加载并执行正常,没有错误。如果我只是在脚本标记中吐出上面的代码(document.write)它可以工作 - PHP回调似乎不起作用?

3 个答案:

答案 0 :(得分:1)

您无法在document.writedefer属性中包含的脚本中使用async。试图这样做是不合逻辑的。

此外,您需要转义字符串中的引号 - 任何带有颜色编码的编辑器都会向您显示属性内容未包含在strine中,除非您在引号内放置\。< / p>

答案 1 :(得分:0)

您没有正确转义PHP文件中的双引号字符(")。它们前面应加反斜杠(\)个字符(请参阅here)。 PHP脚本应为:

 <?php echo "document.write('<div id=\"adblock\" class=\"ha_adblock121_45\" style=\"width:125px;height:125px;background:#eee;text-align:center;cursor:pointer\" onClick=\"window.location.href=click.php?g=http://refererurl.com/;return false;\"><a href=\"http://refererurl.com/\" onClick=\"window.location.href=click.php?g=http://refererurl.com/;return false;\" style=\"color:#333;font-weight:bold\">advert</a></div>');"; ?>

答案 2 :(得分:0)

&LT; ?php echo“&lt; script&gt;(function(){document.write('&lt; div id = \”adblock \“class = \”ha_adblock121_45 \“style = \”width:125px; height:125px; background:# eee; text-align:center; cursor:pointer \“onClick = \”window.location.href = click.php?g = http://refererurl.com/; return false; \“&gt; advert&lt; / a&gt; &lt; / div&gt;');})();&lt; / script&gt;“; ? &GT;

调用自动化函数,该函数将在页面加载时执行..: - )