Reputation: 33542
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
Upvotes: 8
Views: 30678
Reputation: 116
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.
Upvotes: 1
Reputation: 730
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!
Upvotes: 0
Reputation: 2894
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.
Upvotes: 1
Reputation: 43245
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>';
Upvotes: 2
Reputation: 2552
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();
}
Upvotes: 3
Reputation: 2770
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 #
Upvotes: 2