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?

Maigol
  • 7
  • 5
  • 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 – Andrew Lohr Dec 15 '17 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 – Maigol Dec 15 '17 at 20:50
  • https://stackoverflow.com/questions/14446447/how-to-read-a-local-text-file Check answer which suggest using of fetch api, seems promissing... – sinisake Dec 15 '17 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 – Andrew Lohr Dec 15 '17 at 21:14
  • As I read the comments in that answer it seems others have seen the same – Andrew Lohr Dec 15 '17 at 21:15
  • @AndrewLohr, works fine in Firefox...Pity... :) On the other hand, works fine when you acces the file via http protocol: http//localhost/... so, it seems much better/simpler than the rest of the solutions for me... – sinisake Dec 15 '17 at 21:17

2 Answers2

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'/>
Andrew Lohr
  • 5,380
  • 1
  • 26
  • 38
  • Sorry, what is the 2nd line(document.getElementById...) of code for? btw, my script isn't in a html file – Maigol Dec 15 '17 at 21:31
  • that line targets the 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. – Andrew Lohr Dec 15 '17 at 21:46
  • 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 – Andrew Lohr Dec 15 '17 at 21:47
  • 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. – Maigol Dec 15 '17 at 23:39
  • Also... the debug window, says "Error: 'document' is not defined" :s – Maigol Dec 15 '17 at 23:56
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

Andrew Lohr
  • 5,380
  • 1
  • 26
  • 38
Ciro Spaciari
  • 630
  • 5
  • 10