I have an HTML file with JavaScript that I am running without any Webserver/host so I am just opening the file in a browser local to my windows PC. In that HTML file I would like to be able to read a text file in the same folder as the html file. That file will contain data in rows and columns separated with tabs. i.e

1 a 2 b 3 c

I want to keep this as simple as possible so all I have to do is share the HTML and Text file to others so the can open it up local to their computer without any webserver/host and without having to also copy of external libraries like node.js or jquery.

I have searched and tested everything I can find but either I need to reference an external library or I have to run it in a webserver or I need to click a button to load the file through the browser, none of what I want.

Does native JavaScript support the function to read a text file and save it to an array? If so, any code direction would be great.

1 Answers

Mark Minerov On


XMLHttpRequest() exists in native JavaScript, I think it will help you.

You also can send a request to the file. Or use library: axios.js because when you use XMLHttpRequest() you lose many time to write code which just get content from file, with axios I got file content with one line: `axios.get('file.txt').then(result => console.log(result.data));

To connect Axios: <script src="https://unpkg.com/[email protected]/dist/axios.min.js"></script>

You can read official documentation about axios.js and XMLHttpRequest() in the net.