Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read txt file via client javascript

Tags:

javascript

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>
like image 815
Elad Avatar asked Nov 15 '11 13:11

Elad


People also ask

Can I read text file using JavaScript?

The FileReader API can be used to read a file asynchronously in collaboration with JavaScript event handling.

How you can read and write text files in JavaScript?

writeFile() methods are used to read and write of a file using javascript. The file is read using the fs. readFile() function, which is an inbuilt method.


1 Answers

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

like image 154
badfur Avatar answered Oct 07 '22 09:10

badfur