简体   繁体   中英

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?

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.

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM