PHP generated content inside Javascript breaking script

拈花ヽ惹草 提交于 2019-12-13 03:11:42

问题


I have a website with some Javascript rotating content with a fade. The script works fine until I add a PHP while loop inside it, then it fails. I'm using the PHP to pull content from wordpress posts.

here is the isolated code (http://jsfiddle.net/dzz8M/2/). You'll notice the script works as desired if you remove the PHP and you can see here (http://www.smartcallclaims.com/indexx.php/) that the php is outputting fine.

When you check the source of that link and look for the JavaScript from the JSfiddle you'll see the PHP has generated it's content inside the script. (I know it adds a <p> tag, this is not the issue)

Can some please tell me why the PHP breaks the javascript?


回答1:


Your problem is the one unescaped linebreak in the quotes:

"\&nbsp;&nbsp;&nbsp;&nbsp;<p>this is a good web test</p>
<div id='quoteauthor'>Ben Harrison</div>",

This is not valid JavaScript. You have to escape it somehow, for example like by using str_replace() in the PHP functions, that create those strings.

str_replace("\n","&nbsp;",$string)


来源:https://stackoverflow.com/questions/14660300/php-generated-content-inside-javascript-breaking-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!