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.

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.

what…?
why would I need a file system to parse a text file via url?

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.

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:


$.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.

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…

<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…

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.

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.