Html 语法错误:未终止的字符串文字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19269383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: unterminated string literal
提问by Pavan
I have the below code in which i am getting the strange error.
我有以下代码,其中出现了奇怪的错误。
function export_to_excel()
{
results_html = $('report_excel').innerHTML;
results_html = '<html><body><table align="center" cellspacing="0" cellpadding="0" width="100%"><tr><td colspan="9"><b>Employee Salary Register </b></td></tr><tr><td colspan="9"></td></tr>' + results_html + '</table></body></html>';
var input = new Element('input', {
'type': 'hidden',
'name': 'results[html]',
'value': results_html
});
var form = new Element('form', {
'method': 'post',
'name': 'Employee Salary Register',
'action': "html_to_excel"
});
form.insert(input);
document.body.appendChild(form);
form.submit();
}
The error is occurring at this line
错误发生在这一行
results_html = '<html><body><table align="center" cellspacing="0" cellpadding="0" width="100%"><tr><td colspan="9"><b>Employee Salary Register </b></td></tr><tr><td colspan="9"></td></tr>' + results_html + '</table></body></html>';
EDIT:
编辑:
The error is showing in the console of Firebugpointing to that line.I just attached a Screenshot
错误显示在指向该行的Firebug的控制台中。 我只是附上了一个截图
There you can see,under the Two Buttons(print and Export),the code is appearing on the screen.Those code lines are part of the function export_to_excel()
在那里你可以看到,在两个按钮(打印和导出)下,代码出现在屏幕上。那些代码行是功能的一部分 export_to_excel()
It does works perfectly in my Local Server too. I'm Using PrototypeJSand I'm sorry for the Misleading and sorry for the delay too.
它在我的本地服务器中也能完美运行。我正在使用PrototypeJS,对于误导和延迟也很抱歉。
Any help will be greatly appreciated
任何帮助将不胜感激
采纳答案by Piyush Marvaniya
I have implemented your code below, and it works well
First Define your id/class first in this line results_html = $('report_excel').innerHTML;
我已经在下面实现了你的代码,它运行良好首先在这一行中首先定义你的 id/class results_html = $('report_excel').innerHTML;
function export_to_excel(){
results_html = $('report_excel').innerHTML;
results_html = "<html><body><table align='center' cellspacing='0' cellpadding='0' width='100%'><tr><td colspan='9'><b>Employee Salary Register </b></td></tr><tr><td colspan='9'></td></tr>" + results_html + "</table></body></html>";
var input = new Element('input', {
'type': 'hidden',
'name': 'results[html]',
'value': results_html
});
var form = new Element('form', {
'method': 'post',
'name': 'Employee Salary Register',
'action': "html_to_excel"
});
form.insert(input);
document.body.appendChild(form);
form.submit();
}
回答by DarkHorse
Just a suggestion: Change your line from:
只是一个建议:从以下位置更改您的线路:
results_html = '<html><body><table align="center" cellspacing="0" cellpadding="0" width="100%"><tr><td colspan="9"><b>Employee Salary Register </b></td></tr><tr><td colspan="9"></td></tr>' + results_html + '</table></body></html>'
to
到
results_html = "<html><body><table align='center' cellspacing='0' cellpadding='0' width='100%'><tr><td colspan='9'><b>Employee Salary Register </b></td></tr><tr><td colspan='9'></td></tr>" + results_html + "</table></body></html>";
I have replaced "with 'and viceversa.. This would probably remove your error.Hopefully.
我已经取代了“有”,反之亦然。这可能会删除您error.Hopefully。
NOTE: Since in comment you mentioned that $('report_excel')is actually you tableId the correct jquery way to access id is $("#report_excel").. Use #
注意:由于您在评论中提到$('report_excel')实际上是您 tableId,因此访问 id 的正确 jquery 方法是$("#report_excel")..使用 #
回答by DhruvPathak
The problem is with the quotes inside your results_html content.
问题在于 results_html 内容中的引号。
It seems you are using jQuery, (1) If you are :
看来您正在使用 jQuery,(1) 如果您是:
results_html = $('report_excel').innerHTML;
results_html_container = $('<html><body><table align="center" cellspacing="0" cellpadding="0" width="100%"><tr><td colspan="9"><b>Employee Salary Register </b></td></tr><tr><td colspan="9"></td></tr>' + results_html + '</table></body></html>';
results_html_container.html(results_html);
2.If you are not : Escape the single quotes in your content.
2.如果您不是:转义内容中的单引号。
results_html = $('report_excel').innerHTML;
results_html = '<html><body><table align="center" cellspacing="0" cellpadding="0" width="100%"><tr><td
colspan="9"><b>Employee Salary Register </b></td></tr><tr><td
colspan="9"></td></tr>' + results_html.replace(/'/g, "\'"); +
'</table></body></html>';
回答by AO_
Just do this:
只需这样做:
<script type="text/javascript">
function export_to_excel() {
var results_html = $('report_excel').html() ? $('report_excel').html() : "";
results_html = '<html><body><table align="center" cellspacing="0" cellpadding="0" width="100%"><tr><td colspan="9"><b>Employee Salary Register </b></td></tr><tr><td colspan="9"></td></tr>' + results_html + '</table></body></html>';
var input = new Element('input', {
'type': 'hidden',
'name': 'results[html]',
'value': results_html
});
var form = new Element('form', {
'method': 'post',
'name': 'Employee Salary Register',
'action': "html_to_excel"
});
form.append(input);
document.body.appendChild(form);
form.submit();
}
</script>
The code is a bit more readable as well as using proper jQuery instead of a mix-match which causes problems (as in your case).
该代码更具可读性,并且使用适当的 jQuery 而不是导致问题的混合匹配(如您的情况)。
I declared the variable, checked if the element had a value otherwise defaulted to a blank string. Added the input to the form by means of appending.
我声明了变量,检查元素是否有一个值,否则默认为空字符串。通过追加的方式将输入添加到表单中。
回答by mitchell_st
I know I'm late to the party, but I had a similar problem and I'll leave my fix here for anyone else who gets to this by googling the error. I was trying to include a local fallback to cdn hosted angular:
我知道我参加聚会迟到了,但我遇到了类似的问题,我会将我的解决方法留在这里给任何通过谷歌搜索错误来解决这个问题的人。我试图在 CDN 托管的 angular 中包含一个本地后备:
<script> //THIS IS WRONG.
window.angular || document.write('<script src="bower_components/angular/angular.min.js"></script>');
</script>
That was throwing the same unterminated string literal error. It turned out that the end script tag in the document write string was tripping up the parser. Just escape the slash in the quoted closing tag.
那是抛出相同的未终止的字符串文字错误。事实证明,文档写入字符串中的结束脚本标记会触发解析器。只需转义引用结束标记中的斜线即可。
<script> //correct version.
window.angular || document.write('<script src="bower_components/angular/angular.min.js"><\/script>');
</script>
Hat tip: HTML5 Boilerplate Project.
帽子提示:HTML5 样板项目。
回答by Kkk
The solution is to put the javascript code into a separate file (like exportExcel.js) and then include that file from your view. NOT use the tag!
解决方案是将 javascript 代码放入一个单独的文件(如 exportExcel.js),然后从您的视图中包含该文件。不要使用标签!