Load local text file with Js

I have text file - data.txt in the same folder than js file. How to add text by js? Like loadString etc.
Thank you.

1 Like


I could do it without node?

const fileUrl = '' // provide file location

   .then( r => r.text() )
   .then( t => console.log(t) )

more about fetch


Lok up jquery ‘load.’ You can also do it with ajax, but jquery ‘load’ does the same thing and is more succint.

https://www.welookups.com/css/css_link.html lean css online

I solved this problem for myself (wrote a little software to help with my taxes) by using this solution that uses HTML5 and vanilla JS. The javascript may seem like a lot, but it has robust edge-case error-catching. If you want to see what you can get away if you have a completely trustworthy user who will always deliver perfect input, go the center of the nested loops and if statements (copied here for reference):

 var fileReader = new FileReader(); 
 fileReader.onload = function (e) { 
	 var fileContents = document.getElementById('filecontents');
 //filecontents is a div in the html that displays the file.
	 fileContents.innerText = fileReader.result; 
//see the link above to see where the variable fileTobeRead comes from.