1

I've got a javascript for converting the data and save it as a csv file. It looks like this:

function downloadCSV(args) {
    var data, filename, link;
    var type = args.type;
    var fields = Object.keys(json_for_export[0]);
    var csv = json_for_export.map(function(row){
        return fields.map(function(fieldName){
            return '"' + (row[fieldName] || '') + '"';
            });
        });
   csv.unshift(fields);
   csv = csv.join('\r\n')
   if (csv == null) return;
   filename = 'csv' + '_' + args.filename || 'export.csv';
   if (!csv.match(/^data:text\/csv/i)) {
            csv = 'data:text/csv;charset=utf-8,' + '\uFEFF' + csv;
   }
   data = encodeURI(csv);
   link = document.createElement('a');
   link.setAttribute('href', data);
   link.setAttribute('download', filename);
   link.click();
}

It works fine in Chrome Browser but doesn't work in Firefox. What can be a cause of the problem? A check into the console and the data is formatted well into a csv type.

2
  • Which version of Firefox? Commented Jun 9, 2016 at 5:12
  • Do you get any console errors? How is downloadCSV being called? Commented Jun 9, 2016 at 5:36

1 Answer 1

4

You need to append the a element to the DOM before clicking it:

document.body.appendChild(link);
link.click();

And after that you can immediately remove it:

document.body.removeChild(link);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.