Finally, my first question here on stackoverflow. I can guarantee you that I have searched for related answers, but no one fits my problem. I am learning JavaScript now, having a background in .NET and C++, but I can't get this piece of code to do what I expect. I am copying here one of the many versions, with possible a questionable placement of the return statement, but maybe you can help me out with this.
function readFile(file) {
var reader = new FileReader();
reader.onload = readSuccess;
function readSuccess(evt) {
var text = evt.target.result;
return text;
};
reader.readAsText(file);
}
What I try here, is reading a file that is accesed in HTML via a tag input with type="file". Everything goes just well inside the code up to that part. I used Chrome Console and Firebug, to capture the return of the function into a variable in the Console, like:
var x = readFile(aFile);
And what I get is an 'undefined' message. Using breakpoints, I can see the text content in the return statement and it is correctly filled! But the text isn't returned! I've seen questions related, but they get around the problem using innerHTML and other kind of tricks that I do not want to do here, because the result of this function is supposed to be consumed by another JS function, and not to update any part of the DOM.
Thank you very much in advance for your time.