最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - parsing html <script> via PHP isn't working completely - Stack Overflow

programmeradmin1浏览0评论

I find this very strange, must be something I'm doing wrong, but still... I'm working on a page using PHP and TPL files. In my TPL file, there's a place in the footer for some extra lines if needed.

For instance, formchecking with Javascript.

so in PHP I did this:

$foot = "<script type=\"text/javascript\">if(document.getElementById){loadEvents();}</script>";

the $foot variable is then parsed and the result in HTML is this:

<script type="text/javascript">if(document.getElementById)</script>

So {loadEvents();} went missing.

Does anybody see what I'm missing here... I'm seriously not finding it. Did I forget to escape a character or something?

I find this very strange, must be something I'm doing wrong, but still... I'm working on a page using PHP and TPL files. In my TPL file, there's a place in the footer for some extra lines if needed.

For instance, formchecking with Javascript.

so in PHP I did this:

$foot = "<script type=\"text/javascript\">if(document.getElementById){loadEvents();}</script>";

the $foot variable is then parsed and the result in HTML is this:

<script type="text/javascript">if(document.getElementById)</script>

So {loadEvents();} went missing.

Does anybody see what I'm missing here... I'm seriously not finding it. Did I forget to escape a character or something?

Share Improve this question edited Dec 1, 2008 at 15:27 Tomalak 339k68 gold badges546 silver badges635 bronze badges asked Dec 1, 2008 at 14:53 VordrellerVordreller 2,53011 gold badges34 silver badges51 bronze badges 2
  • by TPL files do you mean Smarty? How is the $foot getting into the tpl file? – Tom Haigh Commented Dec 1, 2008 at 15:05
  • Maybe telling people that you use a template engine instead of letting them guess would have been a nice thing to do. – Tomalak Commented Dec 1, 2008 at 15:13
Add a ment  | 

4 Answers 4

Reset to default 8

Obviously the template engine you are using eats away the part in curly braces.

Try something like:

$foot = "{literal}<script type=\"text/javascript\">if(document.getElementById){loadEvents();}</script>{/literal}";

It looks like you're using a template engine like Smarty, which tries to parse anything it finds within braces.

This page from the smarty documentation explains how to make smarty ignore sections it would otherwise parse.

I believe with {} that PHP is expecting a variable within them. I haven't tested this, but try using single quote instead of double-quotes.

If you are using smarty you could use the {literal}.

literal

发布评论

评论列表(0)

  1. 暂无评论