The code shown below reads the contents of a text file “Test.txt” into a string, and then displays that string in the web browser.
UPDATE 2014/02/21 – Some of the commenters have had some trouble getting this to work. First, of course, you’ll need to actually have a test file named “Test.txt” in the same folder as the .html file. Less trivially, to get around browser security features, you may need to access the program through a web server, or else explicitly turn off the relevant security feature (for example, in Chrome, if running from “file://”, you might need to start the browser from the command line with the “–allow-file-access-from-files” switch).
UPDATE 2015/03/02 – Well, since the last update, Google Chrome has deprecated synchronous XMLHttpRequests, which makes my previous version of the code no longer work. Frankly, I’m kind of bitter about it–asynchronous calls are absolutely never what I need, and in my opinion they make a nasty disgusting hash of any code they’re used in. But obviously I must be in some sort of vanishingly small minority in that regard. So, what the heck, I’ve replaced my former code with one that uses an asynchronous call instead. I find myself wishing that modern web programming had a windpipe, so I could shove this down it. Oh, I also added a file picker control to it, so that the user can choose which file gets loaded, rather than it just loading a file named “Test.txt” every time. I guess that probably won’t help readability much either. Whatever, I’m going to go listen to music in my room real loud now.