【发布时间】:2014-12-02 02:42:44
【问题描述】:
我正在使用 jquery mobile 来创建一个网络应用程序... 我想知道阅读文本文件的最佳方法是什么? 目前,我的这些功能工作正常......但我不知道它们之间的区别,哪个更好用?或者哪个最快...
功能 1
function readfileAjax() {
$.get('txt/info.txt', function(txt) {
var lines = txt.split(/\n/);
var randLineNum = Math.floor(Math.random() * lines.length);
var text = lines[randLineNum];
var parts = text.split(/#/);
var fullText = parts[0] + " " + parts[1] + " " + parts[2];
$("#msg").append("<p>" + fullText + "</p>");
});
}
功能2
function readfileHttpRequest() {
var filePath = "txt/info.txt";
xmlhttp = new XMLHttpRequest();
//xmlhttp.overrideMimeType('text/plain');
xmlhttp.open("GET",filePath,false);
xmlhttp.send(null);
var fileContent = xmlhttp.responseText;
var lines = fileContent.split(/\n/);
var randLineNum = Math.floor(Math.random() * lines.length);
var text = lines[randLineNum];
var parts = text.split(/#/);
var fullText = parts[0] + " " + parts[1] + " " + parts[2];
$("#msg").append("<p>" + fullText + "</p>");
}
谢谢
【问题讨论】:
-
了解您正在使用什么。 api.jquery.com/jquery.get
标签: javascript jquery jquery-mobile web-applications