10

我在 JavaScript 中有这个奇怪的未终止字符串文字错误。当我只输出一个像“php”这样的单词时(在 cache_open.handler 变量中)。没有错误。这是脚本,以下工作正常:

        <script>
        var cache_open = {};
        var cache_name_open={};         
        var handler='open';
        cache_open.handler='<pre class="brush: html;">php</pre>';           
        cache_name_open.handler='PHP prepared statement';           
        </script>

但是,当我将代码(html 实体输出的源代码)输出到罪魁祸首变量 cache_open.handler 时,它会在控制台中返回未终止的字符串文字错误。

这是返回错误的示例输出:

        <script>
        var cache_open = {};
        var cache_name_open={};         
        var handler='open';
       cacheObj_open.handler='<pre class="brush: html;">
       &lt;?php
       $stmt = $dbh-&gt;prepare(&quot;SELECT * FROM REGISTRY where name = ?&quot;);
       if ($stmt-&gt;execute(array($_GET['name']))) {
       while ($row = $stmt-&gt;fetch()) {
       print_r($row);
       }
       }
       ?&gt;</pre>';            
    cache_name_open.handler='PHP prepared statement';           
        </script>

起初我以为这只是返回的代码的复杂性(例如包含引号等)。但即使是简单的 HTML 代码也会返回错误:

        <script>
        var cache_open = {};
        var cache_name_open={};         
        var handler='open';
        cacheObj_open.handler='<pre class="brush: html;">&lt;html&gt;
        &lt;body&gt;
        &lt;p&gt;Hello world.&lt;/p&gt;
        &lt;/body&gt;
        &lt;/html&gt;</pre>';           
    cache_name_open.handler='PHP prepared statement';           
        </script>

任何想法是什么导致了错误?任何修改建议都非常感谢,谢谢!

4

1 回答 1

20

Javascript strings can't break across newlines without an escape (\). See this question for detailed answers:

于 2013-01-05T08:48:41.797 回答