1

I have this code, but it only works in Chrome, is there any way i can make it work in IE. It should be downloading a CSV file. or if there are any other method that can export csv file using IE10

            $('#download').on('click', function () {
            function download(filename, text) {
                var pom = document.createElement('a');
                pom.setAttribute('#example1', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
                pom.setAttribute('download', filename);
                pom.click();
            }
        });

 <table id="example1" border="1"  style="background-color:#FFFFCC" width="0%" cellpadding="3" cellspacing="3">
<tr>
    <th>Title</th>
    <th>Name</th>
    <th>Phone</th>
</tr>
<tr>
    <td>Mr.</td>
    <td>John</td>
    <td>07868785831</td>
</tr>
<tr>
    <td>Miss</td>
    <td><i>Linda</i></td>
    <td>0141-2244-5566</td>
</tr>
<tr>
    <td>Master</td>
    <td>Jack</td>
    <td>0142-1212-1234</td>
</tr></table>
<a href="#" id="download">Download</a>

Here is the JSFIDDLE DEMO

Updated: Example 3.1 works fine but i dont know how can i convert the above code to this: Working Example

UPDATED::

            $('#download').on('click', function () {
            var csvContent = $('#example1'); //here we load our csv data 
            var blob = new Blob([csvContent], {
                type: "text/csv;charset=utf-8;",
            });

            navigator.msSaveBlob(blob, "filename.csv")
        });
    });

i get error 0x800a139e - JavaScript runtime error: InvalidStateError

  • What version of IE are you using? – witherwind Feb 07 '14 at 08:16
  • I am using Internet Explorer 10, i've found a working example see updated question. –  Feb 07 '14 at 08:48
  • i wonder example 3.1 is PHP works,anyway http://stackoverflow.com/questions/3665115/ help you. – aki miyazaki Feb 07 '14 at 09:03
  • yhh i have tried some of them already most of them dont support IE and the one that do throws an error. From the updated question you can see one of the error i am getting, not sure if the person is using external library –  Feb 07 '14 at 09:23

1 Answers1

0

on IE11, innerHTML works fine.

<html>
<head>
<script lang="javascript">
function download(){
     var csvContent = document.getElementById("example1").innerHTML; //here we load our csv data 
     var blob = new Blob([csvContent], {
         type: "text/csv;charset=utf-8;",
     });

    var blob = new Blob([csvContent], {
        type: "text/csv;charset=utf-8;",
    });

    navigator.msSaveBlob(blob, "filename.csv")
}
</script>
</head>
<body>
<a href="#" onclick="download()">Download</a>

 <table id="example1" border="1"  style="background-color:#FFFFCC" width="0%" cellpadding="3" cellspacing="3">
<tr>
    <th>Title</th>
    <th>Name</th>
    <th>Phone</th>
</tr>
<tr>
    <td>Mr.</td>
    <td>John</td>
    <td>07868785831</td>
</tr>
<tr>
    <td>Miss</td>
    <td><i>Linda</i></td>
    <td>0141-2244-5566</td>
</tr>
<tr>
    <td>Master</td>
    <td>Jack</td>
    <td>0142-1212-1234</td>
</tr></table>
</body>
</html>
aki miyazaki
  • 493
  • 4
  • 11
  • when i click on a button i get an error: 0x800a1391 - JavaScript runtime error: 'download' is undefined . I know what your trying to do, i've seen this method else where it never works for me it would be helpful if i can find out why? –  Feb 07 '14 at 10:11
  • i see same InvalidStateError on IE11,but insert ".innerHTML" to after table element,works fin on my IE11. – aki miyazaki Feb 07 '14 at 10:13
  • I have inserted `innerHTML` but no luck is it because i am using IE10, but is an error to do with onclick. what if i place my javascript in onclick fucntion? in that way i wont need onclick event in a button? –  Feb 07 '14 at 10:22
  • I think what you need is replace your code to "var csvContent = $('#example1').innerHtml".but if it not work ,you have to search a way to convert $('#example1') to string.for example ,.toString() or .outerHtml;download problem is solved,now problem is how convert text. – aki miyazaki Feb 07 '14 at 10:26