HTML - Display text from a text file

9 posts Page 1 of 1
Contributors
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

In html, is there any way to display some text from a given text file on a webpage? Thanks!
Last edited by Cheatmasterbw on Thu Feb 24, 2011 12:31 am, edited 1 time in total.
http://www.megaapps.tk/
User avatar
Codex
Coding God
Coding God
Posts: 2028
Joined: Wed Mar 31, 2010 5:50 pm

We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

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>
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

thanks! Also, if there is an error reading the text file, what text would be displayed?
http://www.megaapps.tk/
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

It will display the server's error response.
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

is there a way to make it display no text if there is an error?
http://www.megaapps.tk/
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

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.
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

i guess i should be good with that.
http://www.megaapps.tk/
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

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.
9 posts Page 1 of 1
Return to “Tutorials”