用javascript读取文本文件的第一行 [英] Reading first line of a text file in javascript

查看:204
本文介绍了用javascript读取文本文件的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的网络服务器上有一个文本文件,位于 /today/changelog-en.txt 之下,用于存储有关网站更新的信息。每个部分都以一个版本号开头,然后是一个更改列表。

Let's say I have a text file on my web server under /today/changelog-en.txt which stores information about updates to my website. Each section starts with a version number, then a list of the changes.

因此,文件的第一行总是包含最新的版本号, d喜欢用普通的JavaScript (没有jQuery)读出。这是可能的,如果是的话,怎么样?

Because of this, the first line of the file always contains the latest version number, which I'd like to read out using plain JavaScript (no jQuery). Is this possible, and if yes, how?

推荐答案

这应该很简单,使用XHR。像这样的东西可以为你工作:

This should be simple enough using XHR. Something like this would work fine for you:

var XHR = new XMLHttpRequest();
XHR.open("GET", "/today/changelog-en.txt", true);
XHR.send();
XHR.onload = function (){
    console.log( XHR.responseText.slice(0, XHR.responseText.indexOf("\n")) );
};

这篇关于用javascript读取文本文件的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆