Using JavaScript/JQuery to get words from a text file

  • Java
  • Thread starter Jamin2112
  • Start date
  • Tags
    File Text
In summary, the conversation discusses the use of Ajax and jQuery to read lines of a text file into a JavaScript array. The solution is to use a jQuery function call, but there are concerns about accessing the array outside of the function call. The conversation also mentions using a different code without jQuery to achieve the same result.
  • #1
Jamin2112
986
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
  • #3
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
  • #4
.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.
 
  • #5


I would recommend breaking down this problem into smaller parts and understanding each step thoroughly before attempting to use the code. First, it is important to understand what the code is trying to accomplish - reading lines of a text file into a JavaScript array. Then, it would be beneficial to understand the syntax and functionality of the JQuery function being used, as well as the purpose of using Ajax. Once these concepts are clear, it would be easier to modify the code and adapt it to fit your specific needs. Additionally, it may be helpful to consult online resources or seek guidance from someone with a deeper understanding of JavaScript and JQuery to fully understand and utilize the code.
 

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

1. How can I use JavaScript/JQuery to get words from a text file?

To get words from a text file using JavaScript/JQuery, you can first use the $.get() method to retrieve the text file. Then, you can use the split() method to split the text into an array of words. Finally, you can use a for loop or other methods to manipulate and use the words as needed.

2. Can I get words from a text file that is not on my local server?

Yes, you can use $.get() to retrieve a text file from a different server or domain. However, you may run into cross-origin resource sharing (CORS) issues, which can be resolved by setting appropriate headers on the server-side.

3. Is it possible to get only certain words from a text file using JavaScript/JQuery?

Yes, you can use various methods such as slice(), substring(), or substr() to extract specific words from the text file. You can also use regular expressions to match and extract specific patterns of words.

4. How can I handle errors while retrieving a text file using JavaScript/JQuery?

You can use the error callback function in the $.get() method to handle errors while retrieving the text file. Additionally, you can use try-catch blocks to handle potential errors while manipulating the text file data.

5. Can I use JavaScript/JQuery to get words from a text file in different languages?

Yes, you can use JavaScript/JQuery to retrieve and manipulate words from text files in any language. However, you may need to consider character encoding and other language-specific considerations while working with the text file data.

Similar threads

  • Programming and Computer Science
Replies
5
Views
471
  • Programming and Computer Science
Replies
4
Views
764
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
2
Views
706
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
3
Replies
81
Views
5K
Back
Top