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>
6
  • 1
    Is this javascript intended to run in a browser? Commented Nov 15, 2011 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)? Commented Nov 15, 2011 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. Commented Nov 15, 2011 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) Commented Nov 15, 2011 at 13:45
  • Did u read the other questions on the same topic in SO? stackoverflow.com/questions/4950567/… - Duplicate of this. Commented Nov 15, 2011 at 13:45

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

7 Comments

Nice. Please set this answer as accepted answer if it was useful.
@Freek8: Common sense suggests that you cannot use XMLHttpRequest to read files from the client machine.
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?
great answer! Thank you @Freek8! I used your snippet to read a text file in App Inventor puravidaapps.com/read.php
I think by now the File API is also a solution w3.org/TR/FileAPI . A tutorial can be found here: htmlgoodies.com/beyond/javascript/…
|
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.

1 Comment

Thanks all, That was one of the quickest answers i got :)
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

1 Comment

There is no sign of ASP in the question. There is DOM though, which suggests client side JS.
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.

3 Comments

That's true for client side javascript. You can perfectly fine read files on server side javascript. For example node.js.
I found this link which explain how to do it... ehow.com/how_5996745_read-file-html-script-javascript.html BUT it didn't work
Yes, I realized at the moment I posted it. My bad.

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.