9

I'm new to javascript and trying to open a txt file into var and then inject it to html div... I tried to use fopen but I didn't succeed.

<script type="text/javascript">
file = fopen(getScriptPath("info.txt"), 0);


file_length = flength(file);
var content = fread(file,file_length);
var div = document.getElementById("myDiv");
//alert(div);
div.innerHTML = "";
div.innerHTML = content;
</script>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Elad
  • 426
  • 1
  • 8
  • 20
  • 1
    Is this javascript intended to run in a browser? – Darin Dimitrov Nov 15 '11 at 13:43
  • What makes you think JavaScript has an fopen? Where is this text file? Do you mean a text/plain HTTP resource at the URL 'info.txt' (relative to where the document is)? – Quentin Nov 15 '11 at 13:43
  • You try to mix PHP and JavaScript in an inappropriate way. Learn to differ between the two first, then you can go on programming. – yunzen Nov 15 '11 at 13:45
  • Short answer, can't be done this way. Javascript is clientside. Which means you can play around with whatever information you have on your screen. If you want to play around with other resources, you will have to use ajax and some sort of script (php, apx) – OptimusCrime Nov 15 '11 at 13:45
  • Did u read the other questions on the same topic in SO? http://stackoverflow.com/questions/4950567/reading-client-side-text-file-using-javascript - Duplicate of this. – Ashwin Krishnamurthy Nov 15 '11 at 13:45
  • Perhaps you want to do an AJAX load of an external file on the same server? Using jQuery or a similar library makes this easy -- http://api.jquery.com/load/ – Blazemonger Nov 15 '11 at 13:45

4 Answers4

11

Although it says xml request this works perfectly fine for txt files too (server and client side).

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","YOUR_FILE.txt",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseText;
Freek8
  • 694
  • 4
  • 11
  • 24
  • Nice. Please set this answer as accepted answer if it was useful. – Freek8 Nov 15 '11 at 13:54
  • 1
    @Freek8: Common sense suggests that you cannot use `XMLHttpRequest` to read files from the client machine. – Dennis Nov 15 '11 at 13:55
  • The QA says he would like to read a text file with javascript. I assume he is working client side, hence this solution works. Since it works, what is the problem? – Freek8 Nov 15 '11 at 14:18
  • You should not do synch calls, asynchronous is better since it will not lock up the browser. – epascarello Nov 15 '11 at 17:14
  • @Freek8: The problem is that your clarification *server and client side* is wrong, as your method will not allow you to fetch files that are not on the server. And even if you run the script locally (which, as I guess, is what you meant by *working client side*), standard security settings of most browsers will still prevent this. – Dennis Nov 15 '11 at 18:11
  • 1
    great answer! Thank you @Freek8! I used your snippet to read a text file in App Inventor puravidaapps.com/read.php – Taifun Aug 17 '12 at 18:01
  • 1
    I think by now the File API is also a solution http://www.w3.org/TR/FileAPI/ . A tutorial can be found here: http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html – Jakob Runge Aug 05 '13 at 20:12
8

JavaScript has none of the functions you are trying to use.

To read files on the server in JavaScript, you can use XMLHttpRequest.

There is no easy way to read files on the client machine.

Dennis
  • 14,264
  • 2
  • 48
  • 57
6

abandoned question:

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","YOUR_FILE.txt",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseText;

by Freek8

Leandro Barreto
  • 361
  • 2
  • 18
badfur
  • 452
  • 3
  • 8
5

For security reasons, Javascript is made so you can not do this. However, a person has made a workaround which may work and posted it here.

Ok, I realize, it only works for files that are publicly accessbile on the server, which I believe is not what you want to do. Still, if you do find a way, it will be a hack like this, but it could also be fixed to not work at any time.

Bikonja
  • 909
  • 5
  • 15
  • That's true for client side javascript. You can perfectly fine read files on server side javascript. For example node.js. – Darin Dimitrov Nov 15 '11 at 13:45
  • I found this link which explain how to do it... http://www.ehow.com/how_5996745_read-file-html-script-javascript.html BUT it didn't work – Elad Nov 15 '11 at 13:46
  • Yes, I realized at the moment I posted it. My bad. – Bikonja Nov 15 '11 at 13:47