Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: unterminated string literal

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

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

like image 811
Pavan Avatar asked Oct 09 '13 10:10

Pavan


People also ask

What is an unterminated string literal?

"unterminated string literal" means that somewhere a string variable is opened by not closed properly, either because of un-escaped character in it, or a line break, or something like that.

How do you solve an unterminated string constant?

To solve the "Unterminated string constant" error, make sure to enclose your strings in quotes consistently. String literals must be enclosed in single quotes, double quotes or backticks. When writing a multiline string use backticks.

How do you make a string literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.


2 Answers

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();
}
like image 142
Piyush Marvaniya Avatar answered Oct 09 '22 06:10

Piyush Marvaniya


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 #

like image 23
DarkHorse Avatar answered Oct 09 '22 07:10

DarkHorse