I think you are close. The 3rd parameter to JavaScript's XMLHttpRequest open() method is whether the data is requested asynchronously or not. true means the download will start and then some time later finish with the onreadystatechange callback called at the end of the download.
You have a couple options here. You can pass in false instead of true and keep your data processing code after the send() call since that will cause the download to finish before send() returns. This is generally frowned upon because it causes the browser to stop doing any processing until the download completes.
Your other option would be to keep the download asynchronous and move the data processing code into the onreadystatechange callback. This would allow the browser to continue to process other events while the file is downloading.
Another issue I see is that you say var data variable in the outer function and in the onreadystatechange function. This will cause the data variable in the inner function to be a different variable than the outer one. So when you set the inner data, its value will be lost when the function returns. To fix this, remove the var from before the inner function's data so the inner function uses the data defined by the outer function.
Finally, it looks like you are wrapping the data in too many arrays. Putting one [] around the data should be enough.
To recap, here are the two options. Use synchronous download & fix the data scope:
$(function () {
var txtFile = new XMLHttpRequest();
var data;
txtFile.open("GET", "automaticlang_EN.ini", /* async = */ false);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) {
// Makes sure it's found the file.
var allText = txtFile.responseText;
var allTextLines = allText.split(/\r\n|\n/);
data = [allTextLines];
}
};
txtFile.send();
var obj = { width: 700, height: 700, title: "Translation Grid",resizable:true,draggable:true };
obj.colModel = [{ title: "ID", width: 100, dataType: "string" },
{ title: "Translation", width: 200, dataType: "string" }];
obj.dataModel = { data: data };
$("#grid_array").pqGrid(obj);
});
Use asynchronous and move the processing into onreadystatechange:
$(function () {
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "automaticlang_EN.ini", /* async = */ true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) {
// Makes sure it's found the file.
var allText = txtFile.responseText;
var allTextLines = allText.split(/\r\n|\n/);
var data = [allTextLines];
var obj = { width: 700, height: 700, title: "Translation Grid",resizable:true,draggable:true };
obj.colModel = [{ title: "ID", width: 100, dataType: "string" },
{ title: "Translation", width: 200, dataType: "string" }];
obj.dataModel = { data: data };
$("#grid_array").pqGrid(obj);
}
};
txtFile.send();
});