SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using frameworks to access text files

    Could anyone link me to an example of how to read a text file into a javascript variable using mootools or jquery(or explain it yourself)?
    Mootools would be preferred because my jquery framework is a little archaic.

    I can't find a very comprehensive example only directions for how to make requests and the examples don't explain how to use the input.

    Oh and I'll be using this script on my website and the text file will be located on the same server.
    Last edited by anarch; Jul 5, 2011 at 19:54. Reason: Clarification

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    JavaScript running in the browser has no access to any file system (neither the client's nor the server's) from which to read any kind of file.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what...?
    why would I need a file system to parse a text file via url?

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Why would you want to parse a text a file in JS when that same task could probably be accomplished much more efficiently and less convoluted with a server side language? You probably can't find anything on it because most people are smart enough to know the tool to do that job is not client side code. Then again without knowing the details its hard to say, but generally speaking that is something that would be better handled at the server level and masked with an asynchronous request. Though so long as the file locations meets same domain restrictions and is accessible to http I guess there is nothing stopping you from using an asynchronous request to grab the contents of a plain text file in JavaScript. That said, there is likely a better way.
    The only code I hate more than my own is everyone else's.

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by anarch View Post
    what...?
    why would I need a file system to parse a text file via url?
    You said you needed to read a file, but if you're parsing something "via url", that's not reading a file.

    That's making an HTTP request and parsing an HTTP response body. That's something JavaScript can do, unlike reading a file.

    The response body happens to be (I assume) produced by a web server sending you the contents of a text file *it* read. JavaScript doesn't know or care about that.

    As for your original question of how to read the contents into a variable with jQuery:

    Code:
    $.get('url/for/example.txt', function(data) {
      //the contents of example.txt is in the variable "data", do something with it
    });
    Note that the request (to the URL of the text file) must be within the same domain as the webpage the JavaScript is on.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    Why would you want to parse a text a file in JS when that same task could probably be accomplished much more efficiently and less convoluted with a server side language? You probably can't find anything on it because most people are smart enough to know the tool to do that job is not client side code. Then again without knowing the details its hard to say, but generally speaking that is something that would be better handled at the server level and masked with an asynchronous request. Though so long as the file locations meets same domain restrictions and is accessible to http I guess there is nothing stopping you from using an asynchronous request to grab the contents of a plain text file in JavaScript. That said, there is likely a better way.
    Well, mostly the reason I asked about using javascript is because I'm already using a script to parse a YouTube JSON-C feed and display a custom line up of thumbnails which onclick play the video onsite in a custom theater box overlay including an accordion style set of filters.
    I would like to "read" a text file because I'd like to make it easy for the people working on the site who may not be tech savvy to add a word that will be used for sorting or identifying videos for particular displays etc. For example, I need to identify two videos to be marked with "SPECIAL EDITION" when displayed on the page and be played in a special player. The feed's only unique identifier available is the ID index of the array object created with the information from the YT API's response.
    Sure... I could use PHP to parse the text file but I'll still have to call it in JavaScript. What exactly makes JS such a terrible candidate for parsing a text file? Browsers have no problem displaying the contents of a text file, I don't see why javascript wouldn't be able to read one. A text file is nothing more than an unsigned byte array with an EOF at the end...



    Quote Originally Posted by Dan Grossman View Post
    You said you needed to read a file, but if you're parsing something "via url", that's not reading a file.

    That's making an HTTP request and parsing an HTTP response body. That's something JavaScript can do, unlike reading a file.

    The response body happens to be (I assume) produced by a web server sending you the contents of a text file *it* read. JavaScript doesn't know or care about that.

    As for your original question of how to read the contents into a variable with jQuery:

    Code:
    $.get('url/for/example.txt', function(data) {
      //the contents of example.txt is in the variable "data", do something with it
    });
    Note that the request (to the URL of the text file) must be within the same domain as the webpage the JavaScript is on.
    <snip></snip>The differences between "reading" and "parsing" are very miniscule in this case. Did I ever say I need to "open" a text file? did I say anything about writing to one? Did i say I wanted to create a text file? nope. What do you think happens when you open a text file for reading in any language or on any operating system or in any program? It parses binary to text! Magic!
    Please inform me of said knowledge you have about the extreme difference between reading a file and parsing a file in this context <snip></snip>?
    Have you forgotten about ActiveX??? WOW zOMG JAVASCRIPT OPENS A FILE - BUT WAIT THIS IS IMPOSSIBLE!?! -- No I don't want to or would I use an ActiveX object for this...
    Last edited by Paul O'B; Jul 6, 2011 at 09:20. Reason: Rude comments removed

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't know why you took this as some kind of personal attack, judging by the slew of insults thrown my way over 3 channels. I just clarified the terminology and provided an answer for your question. If you're more interested in picking fights than writing your script, then I'm outta here.

  8. #8
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I agree with Dan as his answer is 100% valid in order to read information from a text file, your response is out of line and disrespectful as Dan took the time to help and gave you the answer but you failed to recognize that and attacked him for no reason. If that is how your going to continue how to act then I suggest you don't return as we take the time out of our personal lives to help you build up your development career.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •