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:
-
将JavaScript代码放入
.js
文件并链接到该文件,或者
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 /
.