var xmlObj = false;
var xmlResult;
try {
xmlObj=new XMLHttpRequest;
}
catch(e) {
try {
xmlObj=new ActiveXObject("MSXML2.XMLHTTP");
}
catch(e2) {
try {
xmlObj=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e3) {
xmlObj=false;
}
}
}
if (!xmlObj) {
alert("XMLHttpRequest init Failed!");
}
function AjaxSearch() {
var searchword;
// 获取搜索关键字,并且进行URLEncode
searchword=escape(document.getElementById("searchword").value);
if(searchword=="") {
// 如果关键字为空,则提示用户输入关键字
document.getElementById("search_result").innerHTML="
";
return;
}
// 给出提示,正在搜索
document.getElementById("search_result").innerHTML="";
// 打开一个连接,采用POST
xmlObj.open ("POST", "ajaxsearch.asp", true);
// 设置请求头,表单内容格式为URLEncoded
xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
// 设置完成请求后响应函数
xmlObj.onreadystatechange=function() {
// 完成响应
if(xmlObj.readyState==4) {
// 状态正常
if(xmlObj.status==200) {
// 设置xmlResult为搜索结果XML文档
xmlResult=xmlObj.responseXML;
// 调用AjaxShowResult()显示搜索结果
AjaxShowResult();
}
}
}
// 发送请求,内容为搜索的关键字
xmlObj.send("searchword="+searchword);
}
function AjaxShowResult() {
var results,i,strTemp;
// 获取搜索结果集合
results=xmlResult.getElementsByTagName("result");
// 用无序列表来显示搜索结果
strTemp="
";
// 显示搜索结果
document.getElementById("search_result").innerHTML = strTemp
}
源程序:ajaxsearch.rar