I made a Javascript function for reading csv files like this :
function readTextFile(file) {
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if(rawFile.readyState === 4) {
if(rawFile.status === 200 || rawFile.status == 0) {
alert(rawFile.responseText);
return rawFile.responseText;
}
}
}
rawFile.send(null);
}
The alert inside the function returns exactly what's in the file, but as soon as I use the function in my HTML like this :
<script src='readCsvFile.js'></script>
<script>
alert(readTextFile('../users.csv'));
</script>
The funtion seems to be returning undefined. At least that's what the alert/ consol.log shows.