SyntaxError:unterminated string literal< script> ...< / script>标签不在字符串变量中工作 [英] SyntaxError: unterminated string literal <script>...</script> tag not working within a string variable

查看:53
本文介绍了SyntaxError:unterminated string literal< script> ...< / script>标签不在字符串变量中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看我的代码。

var id = 1;
var htmlText = '<div id="horizontalUserPopup"><ul id="tabs1" class="rtabs"></ul><div class="panel-container"><div id="view1"><script>viewMiniProfile("'+id+'",this);</script></div><div id="view2"></div><div id="view3" style="display:none;">Blah Blah Blah</div><div id="view4" style="display:none;">444444</div></div></div>';
$('#contents').html(htmlText);

以上代码我收到以下错误 -

Above code i am getting following error -

如果我删除< / script> 工作正常。请检查并告知我们。

If i remove </script> its working fine. Please check and let me know.

编辑:

完整代码 -

function modelInfo(id, username) {
                var pageUrl = $('#pageurl').val();
                $('#model-popup-box1 h3').addClass('newsfeed');
                var content_area = $('#model-popup-content1');
                content_area.html('Please wait...');
                $('#model-popup-box-title1').html('About ' + username);
                $('#model-popup-body1').show();

                content_area.html('<div id="horizontalUserPopup"><ul id="tabs1" class="rtabs"></ul><div class="panel-container"><div id="view1"><script>viewMiniProfile("'+id+'",this);</script></div><div id="view2"></div><div id="view3" style="display:none;">Blah Blah Blah</div><div id="view4" style="display:none;">444444</div></div></div>');

                var innerHtml = "<li><a href=\"#view1\" id='miniprofile-view1' onclick=\"viewMiniProfile('"+id+"',this)\">Mini-Profile</a></li>" +
                        "<li><a href=\"#view2\">Tokens</a></li>" +
                        "<li><a href=\"#view3\">Notes</a></li><li><a href=\"#view4\">PM Logs</a></li>";

                var ul = document.getElementById("tabs1");
                ul.innerHTML = innerHtml;


                $('#horizontalUserPopup').responsiveTabs({
                    rotate: false,
                    startCollapsed: 'accordion',
                    collapsible: 'accordion',
                    setHash: true,
                    disabled: [4, 5]
                });

            }


推荐答案

我'我将假设您的引用代码位于脚本标记中,如下所示:

I'm going to assume your quoted code is in a script tag, like this:

<script>
// ...stuff here...
$('#contents').html('<div id="horizontalUserPopup"><ul id="tabs1" class="rtabs"></ul><div class="panel-container"><div id="view1"><script>viewMiniProfile("'+id+'",this);</script></div><div id="view2"></div><div id="view3" style="display:none;">Blah Blah Blah</div><div id="view4" style="display:none;">444444</div></div></div>');
// ...stuff here...
</script>

如果是这样,问题是外部脚本标记在字符串中间终止,因为浏览器中的HTML解析器不解释JavaScript代码,它只扫描它寻找< / script> 找出标签结束的位置。因此,传递给JavaScript解析器的代码是

If so, the problem is that the outer script tag is terminated in the middle of your string, because the HTML parser in the browser doesn't interpret JavaScript code, it just scans through it looking for </script> to figure out where the tag ends. So the code that gets handed to the JavaScript parser is

$('#contents').html('<div id="horizontalUserPopup"><ul id="tabs1" class="rtabs"></ul><div class="panel-container"><div id="view1"><script>viewMiniProfile("'+id+'",this);

...确实有一个未终止的字符串文字。

...which does indeed have an unterminated string literal.

你可以通过以下方式修复:

You can fix that by:


  1. 将您的JavaScript代码放在 .js 文件中并链接到该文件,或者

  1. Putting your JavaScript code in a .js file and linking to it, or

添加反斜杠在字符串中的结束脚本标记中的 / 之前:

Putting a backslash before the / in the ending script tag in your string:

$('#contents').html('...<script>...<\/script>...');

\ 阻止浏览器的解析器看到序列< / script> ,所以它不认为脚本在那里结束。但是,在JavaScript字符串中, \ / 只是 /

The \ prevents the browser's parser from seeing the sequence </script>, and to so it doesn't think the script ended there. In a JavaScript string, however, \/ is just /.

这篇关于SyntaxError:unterminated string literal&lt; script&gt; ...&lt; / script&gt;标签不在字符串变量中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆