ajax|非同步 /*
作者:welfred
時間:05年8月14日
作用:Ajax非同步呼叫架構
*/
var req;
// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function ajax(url){
req = false;
// branch for native XMLHttpRequest object
if(window.XMLHttpRequest) {
try {
req = new XMLHttpRequest();
} catch(e) {
req = false;
}
// branch for IE/Windows ActiveX version
} else if(window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
req = false;
}
}
}
if(req) {
req.onreadystatechange = processReqChange; //Once the state changed,the function "processReqChange" will be excuted.
req.open("GET", url, true);
req.send();
}
}
/*Wait for the response*/
function processReqChange()
{
// only if req shows "complete"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
//Here is your bussiness code
procBusiness();
} else {
alert("獲得資料遇到問題!\n請保證您的網路串連暢通;否則,請及時與管理員聯絡,謝謝!:\n" + req.statusText);
}
}
}
/*
Read the XML file and retrive the data */
function procBusiness(){
var xmlResult = req.responseXML;
var root = xmlResult.documentElement;