Livi17
Livi17

Reputation: 1660

HTML5 with javascript to parse and display textfile

I have an HTML5 page (index.html), and I need to display the contents of a .txt file (p001wide.txt). Both are on the server in the same directory.

The text file is generated from a CMS, so I cannot change the format of that file.

In the .txt file is a variable named widetxt. I need to display the contents of the widetxt variable on the page.

What do I need to do to parse the the textfile and display it in the HTML page?

Do I need to use javascript, and if so, how?

Upvotes: 3

Views: 4634

Answers (3)

JimFuqua
JimFuqua

Reputation: 1071

As both your index file and the text file you wish to read reside on the server you should be able to read the text file on the server and insert what you wish from it into the index file using PHP.

See http://www.w3schools.com/php/php_file.asp for a tutorial on how that can be done without resorting to client side script.

If you cannot alter your index file on the server and can put a PHP file on the server you can use AJAX to ask your PHP file to read the contents of the text file and return it to you. Then you would use javascript to insert it as you wish. I presume that you can alter the index file because otherwise you could not alter its javascript either.

Upvotes: 1

Ryan Kempt
Ryan Kempt

Reputation: 4209

You can do this with JavaScript, you basically need to learn how to read and write files (and you mention the variable inside, this would be parsing). Start reading here - http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm other information, just Google read write files javascript, or parsing text with javascript.

Best of luck!

Upvotes: 0

Adam Ashwal
Adam Ashwal

Reputation: 1472

Hm, I found this question which appears to be similar to yours as far as reading a file goes. As for parsing though why not use a database such as MySQL to store your data? This way you can quickly add and query through your data.

Upvotes: 1

Related Questions