1

I've been searching for this a while now, and though my problem seems a lot easier than comparable problems others are posting, I still can't find a solution.

I would like an array to fill with content from a .txt file. The thing is (opposed to other people having this problem), I don't have to do anything with the contents from the txt file, since it's already in the form of [1, 2, 3], [a, b, c], ... etc. So the only thing I'd like to do is print content from a txt file in

Somewhere between <script> and </script>, there is:

var locations = [
*contents of the txt file should go here*
];

Thats it! Is there a way to grab the contents from a txt file and print them in javascript?

3
  • 1
    Do you need to read that text file from your (a) web-site or from the user computer? Commented Jan 23, 2013 at 21:31
  • 1
    If the text file is hosted then you can perform an XHR request to retrieve the data and then populate your array. Commented Jan 23, 2013 at 21:33
  • This might be useful, however, you need HTML5 html5rocks.com/en/tutorials/file/dndfiles Commented Jan 23, 2013 at 21:33

3 Answers 3

4

Assuming that you're using javascript in a browser context, this is not possible. Security restrictions prevent scripts running in the browser from accessing the host's filesystem.

EDIT: great comments guys! If you're able to use HTML5, then you have some file access capabilities using the FileAPI (draft) standard.

Paul S provided this jsbin that demonstrates: http://jsbin.com/ulodaj/1/

What you'd want to do is ideally get that text file into JSON format, then use the FileAPI to read the string from disk. From there, just locations = JSON.parse(fileText)

EDIT: so if the text file resides in a directory on your PHP site, why don't you use PHP to read the file and write the data as indicated in your OP?

Catch here is that you still want your input file to be valid JSON -- [1, 2, 3], [a, b, c] isn't. A minor change to { foo: [a,b,c], bar: [1,2,3] } would do the trick. Note that I'm not even going to mention using eval because... yeah, it's evil. Don't use it :)

Sign up to request clarification or add additional context in comments.

8 Comments

I was already editing to add FS rather than OS when you posted your comment - you beat me to the punch! the security blocks in place apply even to HTML 5 AFAIK unless you want to get semantic and call indexedDB or localstorage 'file system' access :P
huh. I wasn't aware of that one. Do any browsers implement FileAPI yet?
sweet. I will assume though that the OP intends to read an external (to the script domain) or arbitrary file from the FS, and not one in a security sandbox. Does FileAPI allow for that scenario?
Here you can read the contents of any user selected file to a textarea jsbin.com/ulodaj/1
Sorry for not mentioning; both the javascript and the txt file are server side! That makes it easier?
|
2

Using the File API 's FileReader to access user-selected files on their local machine, which is available on most modern browsers, you could achieve your desired action with something such as the following (example):

var locations;
input.onchange = function () { // `input` some <input type="file">
    var fr = new FileReader();
    fr.onload = function (e) {locations = this.result.split("\n");}; // make this callback your next stage
    fr.readAsText(this.files[0]);
}

Please note that this is event driven so you'll have to use callbacks.

Comments

0

First of all, excuse me for not describing the problem in good detail. I've found the solution to my problem and I'm fairly new here (and not that good of a programmer), but I'll try to explain the answers.

First, the problem: I have php page, on which I have some javascript code in the <head> section. In that javascript I want to load the contents from a txt file. The exact text which is in the txt file is:

var locations = [
['a', 123, 456, 'foo', 'bar'],
[...],
['b', 456, 789, 'bar', 'foo'],
];

I know this isn't exactly a beautiful solution and I know that the var locations []; part should be in the javascript instead of in the txt file, but this works fine for me.

Solution: In the javascript I've put <?php echo file_get_contents('text.txt'); ?> where I want the content of the textfile to show up and that's it!

The reason it solves my problem is because I have to update the array of locations frequently, and the code appears on about 30 pages. I didn't want to have to update all the <head> sections of those pages when there are new locations, so I wanted to put some code in the javascript that grabs the locations from an external file. Now I only have to update one file.

Again, I realize I wasn't describing my problem in a clear way, but I hope that others having the same problem can use this solution!

1 Comment

no problem - a large part of being an effective programmer is learning how to conceptualize and articulate that conceptualization. Unfortunately, like many life skills, it's really only something that comes with hard work and practice. Glad you've found a solution and welcome to the community!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.