Java Using JavaScript/JQuery to get words from a text file

  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    File Text
Click For Summary
The discussion focuses on reading lines from a text file into a JavaScript array, highlighting the use of jQuery's $.get method and the challenges of variable scope with asynchronous calls. The original poster expresses confusion about how to access the 'words' array outside of the $.get function due to its asynchronous nature. A solution is proposed using XMLHttpRequest for synchronous file reading, allowing the 'Words' variable to be populated before it is referenced in the body of the HTML. This method ensures that the data is available when needed, contrasting with the potential timing issues of the jQuery approach. The conversation emphasizes the importance of understanding asynchronous behavior in JavaScript when working with external data.
Jamin2112
Messages
973
Reaction score
12
I was searching around the internet for a procedure to read lines of a text file into a JavaScript array. Most of the procedures I saw used Ajax, which I don't understand yet, so I'll refrain from trying to cut-and-paste code that I don't understand.

I found this

Code:
        $.get('wordsEn.txt', function(myContentFile)
        {
            words = myContentFile.split("\r\n");
        }, 'text');

JQuery function call, which does the trick, but how can I get words into the scope outside of the function call? I want to actually be able to use it.
 
Technology news on Phys.org
You can declare "var words" before the .get call - but I think that's only half the problem.
I haven't used jQuery, but I suspect that "myContentFile" becomes available asynchronously to the rest of the program. So let's say you put the "var words;" and "$.get(..." in the <head> section and a reference to the words array "later" in the body. What will happen is that the reference in the body will happen before the data is ready - that is, before the "function(myContentFile)" is called.

Unless you're already using the jQuery library, I would used code such as this:
Code:
<html>
<head>
<script type="text/javascript">
var Words;
var WordFile = new XMLHttpRequest();
  WordFile.open("GET", "file:///C:/temp/html/words.txt", false);
  WordFile.onreadystatechange =
    function () {
      if(WordFile.readyState === 4)
      {
        if(WordFile.status === 200 || WordFile.status == 0)
        {
          Words = WordFile.responseText.split("\r\n");
        }
      }
    };
  WordFile.send(null);
</script>
</head>
<body>
<script type="text/javascript">
document.write(Words[2]);
</script>
</body>
</html>
 
  • Like
Likes 1 person
.Scott said:
You can declare "var words" before the .get call - but I think that's only half the problem.
I haven't used jQuery, but I suspect that "myContentFile" becomes available asynchronously to the rest of the program. So let's say you put the "var words;" and "$.get(..." in the <head> section and a reference to the words array "later" in the body. What will happen is that the reference in the body will happen before the data is ready - that is, before the "function(myContentFile)" is called.

Unless you're already using the jQuery library, I would used code such as this:
Code:
<html>
<head>
<script type="text/javascript">
var Words;
var WordFile = new XMLHttpRequest();
  WordFile.open("GET", "file:///C:/temp/html/words.txt", false);
  WordFile.onreadystatechange =
    function () {
      if(WordFile.readyState === 4)
      {
        if(WordFile.status === 200 || WordFile.status == 0)
        {
          Words = WordFile.responseText.split("\r\n");
        }
      }
    };
  WordFile.send(null);
</script>
</head>
<body>
<script type="text/javascript">
document.write(Words[2]);
</script>
</body>
</html>

Seems to work. Thanks.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
65
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K