リクエストにクロスブラウザの問題があるため、これを抽象化したライブラリを使用することが賢明です。ただし、必要なコールのスケルトンがあります。
大きなファイルをjavascript変数に格納することはあまり面倒ではありません。あなたがやっていることに注意してください!
var words = [];
var query = "";
function parseText(data) {
// taking care of data
// check null handle errors
var data = data.replace(/\W+/g,' '); // replace non words with spaces
words = data.split(' '); // split and cache this if you need it again without refetching
doSearch(query);
}
function doSearch(query) {
// handle the loop trough the array
// you may save the data into a variable and use regex instead of splitting into an array
}
function handler() {
if(this.readyState == 4 && this.status == 200) {
// so far so good
if(this.responseXML != null && this.responseXML != "")
// success!
parseText(this.responseXML);
else
parseText(null);
} else if (this.readyState == 4 && this.status != 200) {
// fetched the wrong page or network error...
parseText(null);
}
}
query = "someword";
var client = new XMLHttpRequest();
client.onreadystatechange = handler;
client.open("GET", "/remote.txt");
client.send();
なぜ、AJAXはサーバー側ではありませんか? – LouwHopley
解析するファイルの数はいくつですか?それは単なるファイルですか、複数ですか?大きなファイルが多数ある場合、ネットワーク上で急速に重くなる可能性があります。 – Arend
この検索をページごとに1回または何回もやりたいですか?私。どのように効率的である必要がありますか? –