0

I'm just trying to do something that simple. All I want to do is load the contents of a text file into a variable. The text file is only one line and is always a string. I know there are other threads asking the same question, but as of now, the closest answer I have gotten is this:

var client = new XMLHttpRequest();
client.open('GET', '/foo.txt');
client.onreadystatechange = function() {
   alert(client.responseText);
}
client.send();

The problem with this is that I am trying to load the information into a variable. Not send it as an alert. I have tried this:

var string;
var client = new XMLHttpRequest();
client.open('GET', '/foo.txt');
client.onreadystatechange = function() {
   string = client.responseText;
}
client.send();

This does not work either.

This is not a duplicate of this post, as that post is focused on Ajax, and doesn't actually answer how to import the information. I'm not utilizing Ajax. I want the information from the file to be usable elsewhere in the program.

Scrub Nugget
  • 9
  • 1
  • 4
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Sterling Archer Aug 17 '17 at 00:48
  • @SterlingArcher That post is mainly focused on Ajax, and also doesn't actually explain how to load the information from the file into the variable. – Scrub Nugget Aug 17 '17 at 01:02
  • Alert the variable inside the onreadystatechange, if it works, the problem here obviously is the asynchronous call as stated above ^^. You have to wait for the onreadystatechange event to fire before using the assigned variable. – Abana Clara Aug 17 '17 at 01:03
  • @Merigold Neither of them work. The alert creates a blank alert box. – Scrub Nugget Aug 17 '17 at 01:06
  • It will be blank if you alert outside the onreadystatechange – Abana Clara Aug 17 '17 at 01:11
  • @Merigold The alert is inside the onreadystatechange – Scrub Nugget Aug 17 '17 at 01:14

1 Answers1

0

it is evident that you are trying to make an HTTP request to the user's local computer and for obvious reasons - browsers do not allow this! else hackers would be able to read the entire content of a users C drive if they wanted - or worse!

So - you are required to upload the file! i.e. <input type="file" id="uploadFile" /> Check this JSFiddle, you would use the onChange event of the input to read the file

For a similar situation, I used file upload to the server and used PHP/c# (cant remember off the top of my head) which returned the required data - this made things a bit easier actually - the server would receive the file and validate, extract required information and return just the required data - although it seems like more work on the server side - which it is a little bit more you can plan it to make reduce the amount of javascript code you might have to write.

SagarScript
  • 1,145
  • 11
  • 15
  • I have an old broken tablet. I made a webpage that tells me the time and date at all times. It also tells me the local weather. I'm going to mount it on my dorm wall. I also made a program that reads my emails, and exports the date that I next need to move my car from the parking lot it's currently parked in. I'm trying to get the clock to also display whether or not I need to move my car. I'm not hacking. – Scrub Nugget Aug 17 '17 at 01:36
  • Ok I think i need a better understanding of what you are doing. So you have a tablet, you have a webpage and a server somewhere, you access that page from the tablet. You make an AJAX call to a txt file that is also on the net somewhere and the data from the txt file is not useable? if this is correct, what type of data is in the file, can you paste an example? secondly in what way would you like to display the data? – SagarScript Aug 21 '17 at 02:53