0

I have a data file, newline delimited, of a variety of names I want to pull into an array for processing in JavaScript. This set of names will eventually need to be sorted, but right now I'm working on the actual file loading process. Here is my code as it stands right now.

var nameslist = document.baseURI.split('.'); nameslist.pop(); nameslist = nameslist.join('.') + ".dat";
console.log("Attempting to read from file: " + nameslist);

var reader = new XMLHttpRequest() || new ActiveXObject('MSXML2.XMLHTTP');
reader.open("GET", nameslist, false);
reader.send(null);
nameslist = reader.responseText.split('\n');
nameslist.pop();

console.log("Elements read: " + nameslist.length);

As expected with the data file consisting of three names (for the test case) I get the following result in my console log...

"Attempting to read from file: file:///home/username/Desktop/test/test.dat" test.js:11

"Elements read: 3" test.js:19

The problem is I get the following warning and error in my log as well, which I would like to eliminate (as simply ignoring it could lead to issues later down the line)...

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help http://xhr.spec.whatwg.org/ test.js:14

syntax error test.dat:1

From my research into the warning about depreciation, it seems Javascript as a language dislikes synchronous operations, as it slows down the script's running speed. In this case however, I need to have everything loaded before the rest of the script can meaningfully run. I know I could refactor this to put the rest of the script in a function that is called after the data is pulled off responseText, and just use a true on the reader.open line, but is there a better way to get JavaScript to synchronously load the data file in question without having to start making a main() function that is only going to get called once?

As for the rogue error pointing to the first line of the data file, I'm frankly stumped. Any ideas on what could be causing this behaviour?

Thanks in advance for any answers I get from the community.

3
  • 1
    No, there is no way to use SJAX without that warning, and no way to use AJAX without a callback. Notice that it doesn't "slow your script down", but just makes the browser unusable during the load time (which might not be an actual problem at startup - except when the load fails). Commented Mar 4, 2015 at 21:23
  • 1
    My bet on that syntax error is that it tries to interpret the .dat file as xml. Try to set the responseType property Commented Mar 4, 2015 at 21:25
  • @ Bergi: The responseType property cannot be altered, so I guess there's no way to circumvent that error sort of going async. From your own linked page: Note: Starting with Gecko 11.0 (Firefox 11.0 / Thunderbird 11.0 / SeaMonkey 2.8), as well as WebKit build 528, these browsers no longer let you use the responseType attribute when performing synchronous requests. Attempting to do so throws an NS_ERROR_DOM_INVALID_ACCESS_ERR exception. This change has been proposed to the W3C for standardization. Commented Mar 4, 2015 at 21:43

1 Answer 1

1

Solved by rewriting the code as follows:

var nameslist = document.baseURI.split('.'); nameslist.pop(); nameslist = nameslist.join('.') + ".dat";
console.log("Attempting to read from file: " + nameslist);

var reader = new XMLHttpRequest() || new ActiveXObject('MSXML2.XMLHTTP');
reader.open("GET", nameslist);
reader.onloadend = main;
reader.responseType = "text";
reader.send();

function main()
{
 nameslist = reader.responseText.split('\n'); nameslist = nameslist.filter(function(n){return n;}).sort();
 console.log("Elements read: " + nameslist.length);
 // Additional code to be run after the load is complete goes in this block, starting at this line.
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Bergi: Thank you for your assistance on this. I don't like that it -has- to be async in order to work without errors and warnings, but I suppose it needs to be this way to make the browsers running this code happy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.