Document.write(fopen(".txt", "r") {basically}

hi, does anybody know how to :
for( i of fopen(“.txt”,“r”) ){
document.write(i);}
i cant get it to work : | [ i’m trying to write to the document from a .txt file in javascript.] thank you

what environment are you executing this in? node?

not in node. just writing a HTML file with Javascript in it, that’s all :
I want to write an external .txt file into an HTML page using Javascript. i forgot where i saw the instructions : |

You could use an XLHttpRequest, something like this:

var x = new XMLHttpRequest();
x.onreadystatechange = function(){
  if (this.readyState == 4 && this.status == 200) document.getElementById("container").innerText=this.responseText;
}
x.open("GET", "filename.txt", true);
x.send();

Your web page HTML file and your text file need to be uploaded to the same folder on a server for this to work.

Paragraph formatting will be lost (unless you add more code).

Reference:
https://www.w3schools.com/xml/xml_http.asp

Or you have to change the filename to a resolvable and retrievable URL, at least.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.