Search code examples
javascriptjqueryhtmlgetsrc

Is it possible to retrieve text files from HTML app directory without HTTP request or <input>?


I'm working on an HTML/javascript app intended to be run locally.

When dealing with img tags, it is possible to set the src attribute to a file name with a relative path and thereby quickly and easily load an image from the app's directory. I would like to use a similar method to retrieve a text file from the app's directory.

I have used TideSDK, but it is less lightweight. And I am aware of HTTP requests, but if I remember correctly only Firefox has taken kindly to my use of this for local file access (although accessing local images with src does not appear to be an issue). I am also aware of the FileReader object; however, my interface requires that I load a file based on the file name and not based on a file-browser selection as with <input type="file">.

Is there some way of accomplishing this type of file access, or am I stuck with the methods mentioned above?


Solution

  • The browser will not permit you to access files like that but you can make javascript files instead of text files like this:

    text1.js:

    document.write('This is the text I want to show in here.');   //this is the content of the javascript file
    

    Now call it anywhere you like:

    <script type="text/javascript" src="text1.js"></script>