The thing is that:
1) textlines
is not defined anywhere like var textlines;
2) textlines
will be returned when the status is OK
but when you're alerting the value it seems that the line executes before the xml request has finished grabbing the contents and returns. Hence, you should alert the value when you check (==4)
or make a function and pass textlines
so that it would be available only if it's set and ready to be displayed. Code goes like this [TESTED]
<script>
var textlines;
function processFile(fileContent) {
var lines= fileContent.split('\n');
return lines;
}
function printValue(textlines){
alert(textlines);
}
function loadFile(uri) {
var r = new XMLHttpRequest();
r.open('GET', uri, true);
r.onreadystatechange = function() {
if (r.readyState == 4) {
textlines = processFile(r.responseText);
printValue(textlines);
return textlines;
}
}
r.send(null);
return textlines;
}
var lines=loadFile('ss.txt');
</script>
UPDATE:
If you want to use the variable textlines
without making a function and passing, you should have to wait a while until xml request is complete to grab the contents so you could do:
<script>
var textlines;
function processFile(fileContent) {
var lines= fileContent.split('\n');
return lines;
}
function loadFile(uri) {
var r = new XMLHttpRequest();
r.open('GET', uri, true);
r.onreadystatechange = function() {
if (r.readyState == 4) {
textlines = processFile(r.responseText);
return textlines;
}
}
r.send(null);
return textlines;
}
var line=loadFile('ss.txt');
setTimeout(function(){
alert(textlines);
},100);
</script>