最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SyntaxError: unterminated string literal - Stack Overflow

programmeradmin3浏览0评论

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 Firebug pointing to that line.I just attached a Screenshot

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

It does works perfectly in my Local Server too. I'm Using PrototypeJS and I'm sorry for the Misleading and sorry for the delay too.

Any help will be greatly appreciated

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 Firebug pointing to that line.I just attached a Screenshot

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

It does works perfectly in my Local Server too. I'm Using PrototypeJS and I'm sorry for the Misleading and sorry for the delay too.

Any help will be greatly appreciated

Share Improve this question edited Feb 4, 2014 at 4:30 Pavan asked Oct 9, 2013 at 10:23 PavanPavan 33.5k8 gold badges52 silver badges77 bronze badges 14
  • Works in the console of Chrome if you change results_html to a random string without quotes, how does results_html look like? – Henrik Andersson Commented Oct 9, 2013 at 10:32
  • What is report_excel exactly? – Mr Lister Commented Oct 9, 2013 at 13:33
  • 1 @MrLister: It is the id of the table which i have and am using it in the above code to export that data into an xls format. – Pavan Commented Oct 10, 2013 at 5:17
  • To be clear, the above code is working fine in local server but at client side it is not working and that strange error is appearing in the console.Files are exactly same though... – Pavan Commented Oct 10, 2013 at 8:44
  • 2 Looks like it depends on 'report_excel' content. Btw if you're using jQuery you should change .innerHTML to .html(), and if report_excel is the id of some element than add # in front of it $('#report_excel'). – Slobodan Stojanovic Commented Jan 28, 2014 at 10:42
 |  Show 9 more comments

6 Answers 6

Reset to default 3 +25

I have implemented your code below, and it works well First Define your id/class first in this line 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();
}

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.

NOTE: Since in comment you mentioned that $('report_excel') is actually you tableId the correct jquery way to access id is $("#report_excel").. Use #

The problem is with the quotes inside your results_html content.

It seems you are using jQuery, (1) If you are :

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.

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

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

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.

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:

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

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!

发布评论

评论列表(0)

  1. 暂无评论