简体   繁体   English

HTML5用javascript来解析和显示文本文件

[英]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). 我有一个HTML5页面( index.html ),我需要显示.txt文件(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. 文本文件是从CMS生成的,因此我无法更改该文件的格式。

In the .txt file is a variable named widetxt . 在.txt文件中是一个名为widetxt的变量。 I need to display the contents of the widetxt variable on the page. 我需要在页面上显示widetxt变量的内容。

What do I need to do to parse the the textfile and display it in the HTML page? 我需要做什么才能解析文本文件并将其显示在HTML页面中?

Do I need to use javascript, and if so, how? 我需要使用javascript,如果是这样,怎么样?

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. 由于您希望读取的索引文件和文本文件都驻留在服务器上,因此您应该能够读取服务器上的文本文件,并使用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. 请参阅http://www.w3schools.com/php/php_file.asp,获取有关如何在不借助客户端脚本的情况下完成此操作的教程。

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. 如果您无法更改服务器上的索引文件并且可以在服务器上放置PHP文件,则可以使用AJAX请求您的PHP文件读取文本文件的内容并将其返回给您。 Then you would use javascript to insert it as you wish. 然后你可以使用javascript根据需要插入它。 I presume that you can alter the index file because otherwise you could not alter its javascript either. 我认为你可以改变索引文件,因为否则你也无法改变它的javascript。

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? 至于解析为什么不使用像MySQL这样的数据库存储你的数据呢? 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). 你可以用JavaScript做到这一点,你基本上需要学习如何读写文件(你在里面提到变量,这将是解析)。 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. 从这里开始阅读 - http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm其他信息,只需谷歌读写文件javascript,或用javascript解析文本。

Best of luck! 祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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