1

First of all, I'm programming in Javascript, but not for a website or anything like that. Just a .js file in a folder in my PC (later I pass that .js file to other people so they can use it).

Now, I wanted to read a txt file within the same folder as the script and store its content in a variable. I'd like to do something like this: Reading a file and storing it in an array, then splitting up the file everywhere there is a }, Then if a string (input by the user, I already have this covered) contains a substring from the array, it would call a function.

Can you please help me?

6
  • The browser/javascript doesn't have access to the file system. Your web server would have to serve up the json file or have a user explicitly upload the file themselves Commented Dec 15, 2017 at 20:44
  • Thanks for the answer, I'll find a way to store the content somewhere else instead of a txt file :) apart from that... do you know something about the rest of the post? Thanks in advance Commented Dec 15, 2017 at 20:50
  • stackoverflow.com/questions/14446447/… Check answer which suggest using of fetch api, seems promissing... Commented Dec 15, 2017 at 20:55
  • @sinisake I get an error trying that method. Maybe it is outdated. Failed to load file:///C:/Users/ME/Downloads/test.txt: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. from Chrome Commented Dec 15, 2017 at 21:14
  • As I read the comments in that answer it seems others have seen the same Commented Dec 15, 2017 at 21:15

2 Answers 2

1

As we answered the first part of your question in the comments, here is my solution to the second part of your question.

You can add an event listener on the input and check the user input against the values in your array. I may have misunderstood what you exactly mean by "substring"

var myData = ["world","one","two", "blue"];

document.getElementById('theInput').addEventListener('input',checkInput);

function checkInput(){
  var input = this.value;
  if(myData.indexOf(input) > -1){
    console.log("match!")
    // call your function
  }
}
<input id='theInput' type='text'/>

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, what is the 2nd line(document.getElementById...) of code for? btw, my script isn't in a html file
that line targets the <input> element with id theInput and adds an event listened on it. So whenever the input changes it will call checkInput function with the context of the input element.
your script doesn't need to be in a .html file. It can be in its own file and then loaded in the html file using a <script> tag
I mean that the script isn't used in a website, it's not executed via a web browser either (Remember Windows Live Messenger? It's alive again with the Escargot MSN Server, so I'm programming a plugin for Messenger Plus, do you remember it? it used javascript). anyways, The "input" is set by another function in the script.
Also... the debug window, says "Error: 'document' is not defined" :s
0

If not need to run in the broswer you can use node and use fs for read/write files.

Node

Node fs (File System):

If you need to run in the broswer you can use XMLHttpRequest and ajax.

Or use a input type=file and use FileReader

Comments

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.