PHP generated content inside Javascript breaking s

2019-08-18 03:04发布

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条回答
孤傲高冷的网名
2楼-- · 2019-08-18 03:43

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)
查看更多
登录 后发表回答