大家都知道,IE 天生一次只能發送一個 Ajax 請求。若想在單一頁面上用 Ajax 請求多個資源,雖然做得到,但程式碼往往會變得雜亂。本文示範一個「頁面呈現多個快取區塊」的實例,把這個問題優雅地解決。
先準備一個取得 DOM 的小工具函式:
// 取得 DOM
function $(id) { return document.getElementById(id); }
思路很簡單:把要載入的快取都放進一個集合,再迭代這個集合,逐一發出請求。
var cache = { page: "Index", id: "Courses", element: $("Courses") };
// page 為要載入的快取頁面,id 為快取 ID,element 為顯示快取的 DOM 物件
接著建立一個發送 Ajax 請求的方法,我們稱之為 AsyncRequest:
var XMLHttp = null;
function $AsyncRequest(request, callback) {
this.method = request.method != null && request.method.toLowerCase() == "post" ? "POST" : "GET";
this.url = request.url;
this.params = request.params;
this.dataType = request.dataType != null && request.dataType.toLowerCase() == "xml" ? "XML" : "text";
this.async = request.async instanceof Boolean ? request.async : true;
if (callback != null) {
this.success = callback.success;
this.error = callback.error;
if (callback.start != null) callback.start();
}
if (XMLHttp == null) {
if (window.XMLHttpRequest) XMLHttp = new XMLHttpRequest();
else if (window.ActiveXObject) XMLHttp = new ActiveXObject("MSXML2.XMLHTTP") || new ActiveXObject("Microsoft.XMLHTTP");
else return false;
}
var current = this;
XMLHttp.open(this.method, this.url, this.async);
XMLHttp.onreadystatechange = function () {
if (XMLHttp.readyState == 4) {
if (XMLHttp.status == 200) {
if (current.success != null)
current.success(current.dataType == "XML" ? XMLHttp.responseXml : XMLHttp.responseText);
} else {
if (current.error != null)
current.error(XMLHttp.responseText);
}
}
};
if (this.method == "POST")
XMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
XMLHttp.send(this.params);
}
呼叫方式如下:
$AsyncRequest({ url: "https://127.0.0.1", method: "GET", async: true, dataType: "text" },
{
start: function () { /* 開始請求執行 */ },
error: function () { /* 請求錯誤時執行 */ },
success: function (x) { /* 成功,結果為 x */ }
});
// 更簡潔的呼叫
$AsyncRequest({ url: "/default.htm" }, {
success: function (x) { alert(x); }
});
再來建立 loadCache() 方法,用來抓取快取內容並顯示出來:
function loadCache(cache, callback) {
this.requestUrl = "/handler/cacheASHtml.ashx?cache.page=" + cache.page +
"&cache.guid=" + cache.id +
(cache.params != null ? "&" + cache.params : "") + "&" + Math.random();
var nodeName = cache.element.nodeName;
if (nodeName != null && (nodeName == "DIV" || nodeName == "SPAN")) {
var element = cache.element;
} else {
alert('不支援的元素(div,span) ' + nodeName);
return false;
}
$AsyncRequest({ url: this.requestUrl }, {
start: function () { element.innerHTML = "加載中!"; },
success: function (x) { element.innerHTML = x; if (callback != undefined) callback(); }
});
}
載入並顯示單一快取可以這樣呼叫:
loadCache({ page: "Index", id: "NearIPrice", element: $("IPrice") });
請求單個並不難,難的是同時請求多個。先把要請求的快取都放進一個集合:


window.caches = [
{ page: _p, id: "VipSchoolArchive", element: $("VipArchives") },
{ page: _a, id: "DefaultPageVipArchivesRightPart", element: $("VipArchiveAd") },
{ page: _a, id: "DefaultPageVipArchivesBottomPart",element: $("VipArchiveAdBottom") }
];
接著就能一次把這些快取全撈回來了:
loadCacheCollection(window.caches);
function loadCacheCollection(cacheArray) {
cacheArray.reverse();
var s = setInterval(function () {
for (var i in cacheArray) {
loadCache(cacheArray[i], function () {
cacheArray.pop(cacheArray[i]);
if (cacheArray.length == 0) clearInterval(s);
});
}
}, 10);
}