Reputation: 7
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?
Upvotes: 1
Views: 1183
Reputation: 630
If not need to run in the broswer you can use node and use fs for read/write files.
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
Upvotes: 0
Reputation: 5486
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'/>
Upvotes: 1