I am trying open build html(inside js funtion) to show table on a new tab. Now I am trying to add a js function for user to download table in excel format, but as soon as I try to add tag code breaks.
I tried following: Wrote download funtion inside script tag of built html code in js function. Added a new javascript file containing download js function. Added download function in a variable and tried including it in script tag. Used <script> instead of tag.
Following are my code:
var myfunction = function download() {
$("#btnExport").click(function(e) {
e.preventDefault();
var data_type = "data:application/vnd.ms-excel";
var table_div = document.getElementById("table_wrapper");
var table_html = table_div.outerHTML.replace(/ /g, "%20");
var a = document.createElement("a");
a.href = data_type + ", " + table_html;
a.download = "exported_table_" + Math.floor((Math.random() * 9999999) + 1000000) + ".xls";
a.click();
});
};
var html='<html><head><title>Locale Value</title><script>'+myfunction+'</script></head>'+
'<body><h2>'+Header+'</h2><br><br><table> <tr> <th>Key</th> <th>Value</th></tr>';
for (var key in data) {
var eachrow = '<tr>'
+ '<td>' + key + '</td>'
+ '<td>' + data[key] + '</td>'
+ '</tr>';
html=html+eachrow;
}
html=html+'</table><br><br><button id="exportExcel" onclick="download()">Export</button></body></html>';
return html;
}
The problem here is as soon as I close script tag(i.e ), my main script tag (in which all functions are written) is shown as closed. And same appears on screen.