Page 1 of 1

HTML - Display text from a text file

Posted: Wed Feb 23, 2011 11:34 pm
by Cheatmasterbw
In html, is there any way to display some text from a given text file on a webpage? Thanks!

Re: HTML - Display text from a text file

Posted: Thu Feb 24, 2011 12:03 am
by Codex

Re: HTML - Display text from a text file

Posted: Thu Feb 24, 2011 1:01 pm
by mandai
You can use JavaScript to access the file:
Code: Select all
<html>
<head>
</head>
<body>
<textarea id="text"></textarea>
<script>
var req = new XMLHttpRequest();
req.open("GET", "file.txt", false);
req.send();
var textarea = document.getElementById("text");
textarea.innerText = req.responseText;
</script>
</body>
</html>

Re: HTML - Display text from a text file

Posted: Thu Feb 24, 2011 1:55 pm
by Cheatmasterbw
thanks! Also, if there is an error reading the text file, what text would be displayed?

Re: HTML - Display text from a text file

Posted: Thu Feb 24, 2011 2:04 pm
by mandai
It will display the server's error response.

Re: HTML - Display text from a text file

Posted: Thu Feb 24, 2011 2:13 pm
by Cheatmasterbw
is there a way to make it display no text if there is an error?

Re: HTML - Display text from a text file

Posted: Thu Feb 24, 2011 2:31 pm
by mikethedj4
Cheatmasterbw wrote:
In html, is there any way to display some text from a given text file on a webpage? Thanks!
Looks like mandai's got you covered, however let me remind you that html is a markup language (Hence Hypertext Markup Language) if you're unaware of what that means, just Google it, and you should findout.

Re: HTML - Display text from a text file

Posted: Thu Feb 24, 2011 2:39 pm
by Cheatmasterbw
i guess i should be good with that.

Re: HTML - Display text from a text file

Posted: Thu Feb 24, 2011 3:26 pm
by mandai
Cheatmasterbw wrote:
is there a way to make it display no text if there is an error?
Yes you can check the status code with req.status.