Load local text file with Js

Load local text file with Js
0.0 0

#1

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


#2

http://www.tutorialsteacher.com/nodejs/nodejs-file-system


#3

I could do it without node?


#4
const fileUrl = '' // provide file location

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

more about fetch


#5

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


#6

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


#7

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; 
 } 
 fileReader.readAsText(fileTobeRead); 
//see the link above to see where the variable fileTobeRead comes from.