簡體   English   中英

SyntaxError:未終止的字符串文字

[英]SyntaxError: unterminated string literal

我有下面的代碼,其中我遇到了奇怪的錯誤。

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

該行發生錯誤

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>';

編輯:

該錯誤顯示在Firebug的控制台中,指向該行。

屏幕截圖

您可以在其中看到兩個按鈕(打印和導出)下的代碼出現在屏幕上。這些代碼行是函數export_to_excel()

它的確在我的本地服務器中也能完美工作。 我正在使用PrototypeJS ,對於誤導和抱歉也感到抱歉。

任何幫助將不勝感激

只是一個建議:更改以下路線:

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>'

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>";

我已經取代”,反之亦然。這可能會刪除您error.Hopefully。

注意:由於在評論中您提到$('report_excel')實際上是tableId,因此訪問ID的正確jquery方法是$(“#report_excel”) .. 使用#

我已經在下面實現了您的代碼,並且效果很好。首先,在此行中首先定義您的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();
}

問題出在results_html內容中的引號。

看來您正在使用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.如果不是,請:在內容中轉義單引號。

   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>';

只是這樣做:

<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>

該代碼更具可讀性,並且使用適當的jQuery而不是導致問題的混合匹配(如您的情況)。

我聲明了變量,檢查元素是否有一個值,否則默認為空字符串。 通過追加將輸入添加到表單中。

我知道我參加晚會很晚,但是我遇到了類似的問題,我將把解決方法留給所有通過谷歌搜索錯誤來解決此問題的人。 我試圖包括本地回退到托管角度的cdn:

<script> //THIS IS WRONG.
window.angular || document.write('<script src="bower_components/angular/angular.min.js"></script>');
</script>

那將引發相同的未終止的字符串文字錯誤。 事實證明,文檔寫入字符串中的結束腳本標簽使解析器崩潰了。 只需在引號中將斜杠轉義即可。

<script> //correct version.
window.angular || document.write('<script src="bower_components/angular/angular.min.js"><\/script>');
</script>

帽子提示: HTML5 Boilerplate項目。

解決方案是將javascript代碼放入單獨的文件(例如exportExcel.js),然后從您的視圖中包含該文件。 不要使用標簽!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM