SyntaxError: line break

I have the code below in which I get a weird 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(); } 

Error in 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 displayed in the Firebug console pointing to this line. I just added a screenshot

Screenshothot

Here you can see that under two buttons (print and export) the code appears on the screen. These lines of code are part of the export_to_excel() function

It works fine on my local server. I am using PrototypeJS and I apologize for the misrepresentation and sorry for the delay.

Any help would be greatly appreciated.

+7
javascript html prototypejs ruby-on-rails-3
source share
6 answers

I have executed your code below and it works well First First, define your identifier / class 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(); } 
+2
source share

Just a sentence: Change your 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>' 

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 replaced ' with ' and vice versa. This will probably fix your mistake. I would like to.

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

+2
source share

The problem is the quotation marks inside your results_html content.

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

 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: Avoid 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>'; 
+2
source share

Just do the following:

 <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 the right jQuery instead of a combination that causes problems (as in your case).

I declared a variable, checked if the element had a value that otherwise would have been empty by default. Added input to the form by adding.

0
source share

I know I'm late to the party, but I had a similar problem and I will leave my fix here for anyone who gets to this by running an error. I tried to include local reserve in hosted cdn angular:

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

This produced the same inexhaustible literal string error. It turned out that the end tag of the script in the document record line included a parser. Just avoid 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.

0
source share

The solution is to put the javascript code in a separate file (e.g. exportExcel.js) and then include that file from your view. DO NOT use the tag!

-one
source share

All Articles