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 | Show 9 more comments6 Answers
Reset to default 3 +25I 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!
results_html
to a random string without quotes, how doesresults_html
look like? – Henrik Andersson Commented Oct 9, 2013 at 10:32.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