JAVASCRIPT完全自学手册,中源码的验证修订实例
源代码在线查看: 22-2 简单的ajax应用模型.hta
22-2 简单的Ajax应用模型
* { font-size:12px; font-family:宋体, Arial; } /*规定了所有的字体样式*/
body { background-color:buttonface; border-style:none; overflow:auto; }
fieldset { padding:10px; }
#txtURL { width:400px; }
#divState { height:70px; background-color:white; border:1px solid black; overflow:auto; margin:10px; }
#divResult { height:100px; background-color:white; border:1px solid black; overflow:auto; margin:10px; }
var xmlhttp;
function $(str){ return(document.getElementById(str)); }
function getXMLRequester(){
var xmlhttp_request = false;
try{
if( window.ActiveXObject ){
for( var i = 5; i > -1; i-- ){
try{
if( i == 2 ){
xmlhttp_request = new ActiveXObject( "Microsoft.XMLHTTP" );
}else{
xmlhttp_request = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
}
break;
}catch(e){
xmlhttp_request = false;
}
}
}else if( window.XMLHttpRequest ){
xmlhttp_request = new XMLHttpRequest();
}
}catch(e){
xmlhttp_request = false;
}
return xmlhttp_request;
}
function start_navigation(){
var url;
url = $("txtURL").value;
if(!url)return;
xmlhttp = getXMLRequester();
xmlhttp.onreadystatechange = xmlhttp_onreadystatechange;
xmlhttp.open("GET", url, true);
log("到\""+url+"\"的连接已打开");
xmlhttp.send("");
log("数据已发送");
}
function xmlhttp_onreadystatechange(){
log("载入中,XMLHTTP状态:" + xmlhttp.readyState);
if(xmlhttp.readyState==4){
log("载入完成,服务器返回码\""+xmlhttp.statusText+"\"");
if(xmlhttp.status==200){
$("divResult").innerText = xmlhttp.responseText;
}
}
}
function log(str){
var obj = document.createElement("div");
var dt = new Date();
obj.innerHTML = "["+dt.toLocaleTimeString()+"]: "+str;
$("divState").appendChild(obj);
}
window.onload = function(){
$("cmdGo").onclick = start_navigation;
}
输入 - URL
地址:
输出 - 状态信息
输出 - 服务器返回的数据