Stackoverflow Response: Export to CSV using jQuery and html

http://stackoverflow.com/questions/16078544/export-to-csv-using-jquery-and-html

HTML

<button id="download">Download CSV</button>   
<hr>

    <table id="importantData" border='1'>
        <tr>
            <th>Column One</th>
            <th>Column Two</th>
            <th>Column Three</th>
        </tr>
        <tr>
            <td>row1 Col1</td>
            <td>row1 Col2</td>
            <td>row1 Col3</td>
        </tr>
        <tr>
            <td>row2 Col1</td>
            <td>row2 Col2</td>
            <td>row2 Col3</td>
        </tr>
        <tr>
            <td>row3 Col1</td>
            <td>row3 Col2</td>
            <td>row3 Col3</td>
        </tr>
        <tr>
            <td>row4 'Col1'</td>
            <td>row4 'Col2'</td>
            <td>row4 'Col3'</td>
        </tr>
        <tr>
            <td>row5 &quot;Col1&quot;</td>
            <td>row5 &quot;Col2&quot;</td>
            <td>row5 &quot;Col3&quot;</td>
        </tr>
        <tr>
            <td>row6 "Col1"</td>
            <td>row6 "Col2"</td>
            <td>row6 "Col3"</td>
        </tr>
    </table>
    
<hr>

CSS

#download3 { background: #eee; border: 1px dashed; text-align:center; padding: 1em;}

JavaScript

$('#download').click(function(){   
    exportTableToCSV( $('#importantData') , 'importantData.csv' );
});
function exportTableToCSV($table, filename) {

    var $rows = $table.find('tr:has(td),tr:has(th)'),

        // Temporary delimiter characters unlikely to be typed by keyboard
        // This is to avoid accidentally splitting the actual contents
        tmpColDelim = String.fromCharCode(11), // vertical tab character
        tmpRowDelim = String.fromCharCode(0), // null character

        // actual delimiter characters for CSV format
        colDelim = '","',
        rowDelim = '"\r\n"',

        // Grab text from table into CSV formatted string
        csv = '"' + $rows.map(function (i, row) {
            var $row = $(row),
                $cols = $row.find('th,td');

            return $cols.map(function (j, col) {
                var $col = $(col),
                    text = $col.text();

                return text.replace(/"/g, '""'); // escape double quotes

            }).get().join(tmpColDelim);

        }).get().join(tmpRowDelim)
            .split(tmpRowDelim).join(rowDelim)
            .split(tmpColDelim).join(colDelim) + '"',

        // Data URI
        //blob = new Blob([csv], { type: 'text/csv' }); //new way
        //var csvUrl = URL.createObjectURL(blob);
        
       csvUrl = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
    
        var link = document.createElement("a");
        link.download = filename;
        link.href = csvUrl;
        link.click();
        link.remove();
    /*
    $('<a>').attr({
    href: csvUrl,
    download: filename,
    style : 'visibility:hidden;'
    }).appendTo('body').trigger('click').remove();
    
    */
        
}