#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    33
    Rep Power
    3

    Reading in from .txt file


    OK Ive been researching a decent amount and Im very aware that for security purposes javascript access to files is for the most part not doable. However, most cases I have found are for people trying to access user files and use them for input. I simply want to read from one .txt file that will already be stored in the same location as the .html file that will never change. Is this still not possible? Id prefer not to use Ajax or ActiveX or stuff like that.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,921
    Rep Power
    1045
    Hi,

    it's possible with Ajax. However, it really makes no sense to load and parse a plain text file with static content. Why don't you simply embed the data in JavaScript code and load it as a standard script?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0
    I can think of some cases why you might want to load a text file. But the question is, why are you sure you need to *load* the file with Javascript? There are various ways to get the file into the page, and from there you can just let Javascript *process* it.

    Method 1: If the data isn't going to change, enter it into the page in a hidden <textarea>:

    <textarea id=mydata_ style="display:none">
    line 1
    line 2
    line 3
    </textarea>

    Pull the data out with: mydata = document.getElementById('mydata_').value;

    Method 2: If your data is changing and so you need to store it in a text file, pull it out with SSI:

    <textarea id=mydata_ style="display:none">
    <!--#include virtual="mydata.txt"-->
    </textarea>
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,921
    Rep Power
    1045
    Originally Posted by MichaelBluejay
    I can think of some cases why you might want to load a text file.
    Namely? Parsing some home-made data format is just slow, cumbersome and error-prone. Native JavaScript code will be parsed for free.

    And that textarea approach I find much too obscure.
    Last edited by Jacques1; February 4th, 2013 at 04:36 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    33
    Rep Power
    3
    Thanks for the replies, it certainly isnt necessary to read them from a file but what I have to work with is a list of 100+ zip codes that I need to be put into an array onload. I could just hardcode those values in but I just think the code would look cleaner if I were to have a few lines parse it into the array. Doesnt really matter though I can just do it the first way.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0
    harrjm, you're right: It's a lot cleaner to separate the data from the code. That's the whole principle behind CSS, separating the content from the formatting, so there's certainly a huge precedent for it. And that's why I knew about the <texatarea> trick; it's what I came up with to solve the very problem you're trying to tackle.

    Jacques1, I could give examples of why you'd want to keep the data separate from the script, but it seems to me that you're just looking for excuses to criticize others' approaches, so I'll decline.
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,921
    Rep Power
    1045
    Originally Posted by MichaelBluejay
    Jacques1, I could give examples of why you'd want to keep the data separate from the script, but it seems to me that you're just looking for excuses to criticize others' approaches, so I'll decline.
    Um, if you don't want your suggestions to be discussed and criticized, then I wonder why you're here. I thought discussing different approaches in order to find the best solution is the whole point of a forum.

    OK, you said you want to separate data and application logic. But when you stuff the data in a textarea within the HTML file, isn't that the exact opposite? I mean, there's no separation at all. Whenever you want to change the data, you'll have to go through all the HTML stuff. So with your approach, you neither have separation (like with an external txt file) nor automatic parsing (like with a JavaScript file). So I don't really see why this should be the best solution.

    Embedding data in JavaScript is a standard approach. JSONP does that. If you rather want plain data, I'd use an external JSON file, fetch it through AJAX and have it parsed by the built-in JSON parser.

    I think those are the two sensible solutions. I'd also consider putting the data in a database, because that will make it much more robust and easier to change.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0
    Jacques1, I'm happy for others to discuss and criticize my ideas sincerely, but your motivation appears quite different. Go back and read your reply #4. You asked for an example of why someone would want to use this approach, AND THEN BEFORE YOU EVEN GOT AN ANSWER YOU PROCEEDED TO DISMISS THE WHOLE IDEA. That tells me that you have no intention of evaluating ideas on their merits.

    Ditto for this:

    OK, you said you want to separate data and application logic. But when you stuff the data in a textarea within the HTML file, isn't that the exact opposite? I mean, there's no separation at all.
    Quite wrong. As you see with my Method #2 above, the data is indeed separate. A server-side script can generate the file from a MySQL database, for example.

IMN logo majestic logo threadwatch logo seochat tools logo