There is a text file with the following content:
User:root
Password:root123
My goal is to read this text file in JavaScript line by line and store it in an array. Then, I want to split each value in the array using a colon (:
).
Despite trying multiple approaches, I haven't been successful in achieving this.
xmlHttp.onreadystatechange = function() {
var tmpDoc, re=/^(.+)$/gm, arr=[], oP, arrSplit=[], arrSpl=[];
if(xmlHttp.readyState === 4) {
if(xmlHttp.status === 200){
alert("AS");
tmpDoc = xmlHttp.responseText;
arrSpl = tmpDoc.split(",");
var arrGlobalHost = arrSpl[0].split(':');
var arrGlobalUser = arrSpl[1].split(':');
var arrGlobalPass = arrSpl[2].split(':');
strGlobalHost = arrGlobalHost[1];
strGlobalUser = arrGlobalUser[1];
strGlobalPass = arrGlobalPass[1];
xmlHttp=null;
}
}
};
xmlHttp.open("POST", fileName, true); // Use POST to prevent use of cached file
xmlHttp.send();
I have attached this function to a button click event, but I'm not getting the desired output on the first click. Since the ready state is only maintained after the page has loaded for the first time, I now need to load the page for the first time itself to ensure it works as intended.