I've searched for an answer for my particular predicament and I reckon it must have something to do with misunderstandings of my own (I'm new to JS). I know of similar topics but not quite addressing what I'd like to pin down - please forgive me if I have missed something previously posted.
I have boiled down what I'm trying to do to the following JavaScript which draws on jQuery's get to read file contents to a variable (I was trying to avoid all the hullabaloo involved in using XMLHttpRequest() just to read a file server-side):
window.onload = function(){
var refFile = "./myFile"; // path to my txt file
var fileContent;
$.get(refFile, function(response) {
fileContent = response;
alert(fileContent); // I get data here just fine
});
alert(fileContent); // undefined here - how can I have fileContent defined here?
}
If it's preferable to use XMLHttpRequest in pure JS instead that's fine, but I'd just like to know how to be able to make the value live outside the retrieving function - for use with other functions, for instance.
Any ideas on what I'm missing?
Any help would be much appreciated.