If your file is located on an http server, you can read it using AJAX.
Let us first define some constants:
var CSV_URL = "http://server.domain/path/file.csv"; var CSV_COLUMN = ';' var CSV_ROW = '\n'
CSV_URL is the URL of your CSV file.
CSV_COLUMN is a delimiter character that separates columns.
CSV_ROW is a delimiter character that selects lines.
Now we need to execute an AJAX request to get the contents of the CSV data. I am using jQuery to execute AJAX queries.
$.get (CSV_URL, null, function (data) { var result = parse_csv (data); var e = create_select (result); document.body.appendChild (e); });
Ok, now we need to analyze the data ...
function parse_csv (data) { var result = new Array(); var rows = data.split (CSV_ROW); for (var i in rows) { if (i == 0) continue;
... and create a selection:
function create_select (data) { var e = document.createElement ('select'); for (var i in data) { var option = document.createElement ('option'); option.value = data[i].value; option.innerHTML = data[i].text; e.appendChild (option); } return e; }
Everything except the AJAX request is pure JavaScript. If you don't want jQuery for any reason, you can also write your AJAX request in pure JS.
source share