您的位置:首页 > 理论基础 > 计算机网络

Ajax 同一页面同时执行多个 XMLHTTP

2008-07-02 09:29 459 查看
<html>
<head>
<title></title>
<script language="javascript">
/*不需要全局变量
//var _url;
//var _divid;
//var xmlHttp;
*/
function createXmlHttp()
{
var xmlHttp;
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("microsoft.XMLHTTP");
}
else if(window.xmlHttpRequest)
{
xmlHttp=new xmlHttpRequest();
}
return xmlHttp;
}
function callServer(url,divid)
{
var divid=divid;
var xmlHttp=createXmlHttp();
xmlHttp.open("GET",url,true);
//xmlHttp.onreadystatechange=updatePage;
xmlHttp.onreadystatechange=function () { updatePage(xmlHttp,divid); };
xmlHttp.send(null);
}
function updatePage(xmlHttp,divid)
{
if(xmlHttp.readystate<4)
{
document.getElementById(divid).innerHTML="loading...";

}
if(xmlHttp.readystate==4)
{
if(xmlHttp.status==200)
{
//alert(_divid);
//document.getElementById("load").innerHTML=xmlHttp.responseText;
document.getElementById(divid).innerHTML=xmlHttp.responseXML.xml+"<br>";
//alert(_divid);
}
}
}

/*
var xmlHttp2;
function createXmlHttp2()
{
if(window.ActiveXObject)
{
xmlHttp2=new ActiveXObject("microsoft.XMLHTTP");
}
else if(window.xmlHttpRequest)
{
xmlHtt2=new xmlHttpRequest();
}
}

function callServer2()
{
createXmlHttp2();
//alert(xmlHttp);
xmlHttp2.open("GET","index_getPro.asp",true);
xmlHttp2.onreadystatechange=updatePage2;
xmlHttp2.send(null);
}

function updatePage2()
{
if(xmlHttp2.readyState<4)
{
document.getElementById("load2").innerHTML="loading2..."
}
if(xmlHttp2.readyState==4)
{
if(xmlHttp2.status==200)
{
//alert(xmlHttp.responseText);
// document.getElementById("load").innerHTML=xmlHttp.responseText;
document.getElementById("load2").innerHTML=xmlHttp2.responseXML.xml;
//document.getElementById("load").innerHtml="ok";
}
}
}
*/

</script>

</head>
<body onload=callServer("index_getNews.asp","load");callServer("index_getPro.asp","load2");>
<div id="load">load</div><br /><br /><br /><br /><br />
<div id="load2">load2</div>
<input type="button" name="button" value="tijiao" onclick=callServer("index_getNews.asp","load");>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: