javascripthtmltext-filestxtjavascript-import

How do I get the data of a .txt file using js?


I need a dictionary for my word game. I have it downloaded and I know the storage path. I have looked for an hour or two(with a couple short breaks) looking for solutions. They either didn't work or were too complicated for me to implement, although I did attempt most of them. I want it in an array-type format. Every new word is on a new line, so split can be '\n'. The dictionary is in the same folder as my js file, but if I need the full path, just use fullPath please. Use dictionary.txt for just the file.

I wish I had some code to get started, but as I said before, none of the ones I've tried work, and I have never done this before. Please help, it is necessary for my game.


Solution

  • I found something as I was getting links. the code is as follows:

    var client = new XMLHttpRequest();
      client.open('GET', '/scriptAndBank/dictionary.txt');
      client.onreadystatechange = function() {
        const dictionary = (client.responseText).split('\n');
        console.log(dictionary);
      };
    client.send();