Convert JavaScript variable value to csv file

I have a comma separated variable in my .js file, for example:

var out='';

I am displaying this value in a browser using document.write(out);.
I would like the out variable to be downloadable as a .csv file.

From data stored in a variable, is there any way to create a csv file and its associated download link in JavaScript?




Depends on browser support, but that's getting pretty good with new browsers:

var CSV = [

window.URL = window.webkitURL || window.URL;

var contentType = 'text/csv';

var csvFile = new Blob([CSV], {type: contentType});

var a = document.createElement('a'); = 'my.csv';
a.href = window.URL.createObjectURL(csvFile);
a.textContent = 'Download CSV';

a.dataset.downloadurl = [contentType,, a.href].join(':');


So the first item is the Blob object, this creates the object that can be downloaded. (

The next part is the download attribute of the link, which informs the browser to download the CSV file rather than opening it in the browser window. (


there is jquery plugin for output file at the client side without server side interaction,


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.