SyntaxError:未终止的字符串文字& lt; script& gt; ...& lt//script& gt;标记在字符串变量中不起作用 [英] SyntaxError: unterminated string literal <script>...</script> tag not working within a string variable

查看:89
本文介绍了SyntaxError:未终止的字符串文字& lt; script& gt; ...& lt//script& gt;标记在字符串变量中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看我的代码.

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]
                });

            }

推荐答案

我假设您引用的代码在script标记中,如下所示:

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>

如果是这样,则问题在于外部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:未终止的字符串文字&amp; lt; script&amp; gt; ...&amp; lt//script&amp; gt;标记在字符串变量中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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