How Can I Get Information From A Local Text File Without Browsing?
Solution 1:
To read a file from the user's disk you need to use FileReader
and the user must explicitly select the file using a file input. (See JavaScript read file without using input).
To read a a file from the website you need to use Ajax (with fetch
, XMLHttpRequest
or a library that wraps around them like Axios). (See Using fetch from MDN).
If (as it seems here) you want to read data from the websitebut the website exists only on the user's disk then you still need to use Ajax but will usually run into security restrictions. Some browsers allow you to disable the security protection, but the general solution is to install a web server and load both HTML and the data file using HTTP.
Alternatively, you can store your data in JavaScript (you are generating an array from your text file, you can so that manually or have a build-time script do it) and just load it with a <script>
element.
Post a Comment for "How Can I Get Information From A Local Text File Without Browsing?"