Loading a .txt File into a JavaScript Variable without Selecting

216 Views Asked by At

I've asked this question before but did not explain it well enough so here's a better attempt:

I am making an application that will be run client-side, not server side. I have a .txt file in the directory with all of the code for the application, and I want to be able to automatically load the contents into a variable without the user needing to select anything. Thus, I do not want to use an input, but rather just have the file load itself when the .html page is opened. As an end result, I would want a String that contains the text inside the file.

I am not using any frameworks, just strictly coding in JavaScript, CSS, and HTML.

I assume that the answer to this question involves a Blob and using readAsText(), but even after reading the full documentation I'm unsure how to pass a URL into it so it can read the contents of a .txt file.

0

There are 0 best solutions below