我想创建一个JavaScript字符串来保存一个动态php页面
newwindow=window.open();
newdocument=newwindow.document;
newdocument.write(HTMLstring);
newdocument.close();
功能。当我尝试插入页面的第一部分时它会启动一个PHP块
HTMLstring+='<?php';
HTMLstring+='echo "this should print";';
HTMLstring+='?>';
我试着从
开始 HTMLstring+='<'?php';
但是我得到
<!--?phpecho "this should print";?-->
在JavaScript中有办法做到这一点吗?
thank you
你误解了JavaScript和PHP的工作方式。PHP在服务器上运行,JavaScript在客户端运行。使用JavaScript创建PHP代码基本上是不可能的。
在您的情况下,应该可以使用PHP直接将字符串输出到JavaScript中(如
)。<script> var myHTML = "<? echo $variable; ?>"; </script>
当您需要从JavaScript中访问PHP时,另一个常见的解决方案是Ajax。