一段可以判断浏览器的ajax的js代码


var objRequest;
function SendValue(val)
{
//初始化
try
{//MS IE浏览器
objRequest=new ActiveXObject("Msxml2.XMLHTTP");}
catch(e)
{
try
{//所有的非IE浏览器
objRequest=new ActiveXObject("Microsoft.XMLHTTP");}
catch(oc)
{objRequest=null;}
}
if(!objRequest&&typeof XMLHttpRequest!="undefined")
{objRequest=new XMLHttpRequest();}
//处理请求的Web页面
var url=" http://localhost/VBNETSample/HandleAjaxRequests.aspx?sStringIn =" + val;
if(objRequest!=null)
{ objRequest.onreadystatechange = Process;
objRequest.open("GET", url, true);
objRequest.send(null);
}
}
function Process()
{
if (objRequest.readyState == 4)
//值"4"意味着,我们现在可以使用XMLHttpRequest返回的数据
{ if (objRequest.status == 200)
{ document.getElementById("txtEchoOutPut").innerText = objRequest.responseText;
//IE
//document.getElementById("txtEchoOutPut").innerHTML = objRequest.responseText;
//另外的浏览器
}
else
{ document.getElementById("txtEchoOutPut").innerHTML= "There was a problem retrieving
data:<br>" + objRequest.statusText;}
}
}
var  objRequest; function  SendValue(val) {//初始化try{//MS IE浏览器objRequest=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{//所有的非IE浏览器objRequest=new ActiveXObject("Microsoft.XMLHTTP");}catch(oc){objRequest=null;}}if(!objRequest&&typeof XMLHttpRequest!="undefined"){objRequest=new XMLHttpRequest();}//处理请求的Web页面var url="http://localhost/VBNETSample/HandleAjaxRequests.aspx?sStringIn=" + val;if(objRequest!=null){ objRequest.onreadystatechange = Process;objRequest.open("GET", url, true);objRequest.send(null);}}function Process(){if (objRequest.readyState == 4)//值"4"意味着,我们现在可以使用XMLHttpRequest返回的数据{ if (objRequest.status == 200){ document.getElementById("txtEchoOutPut").innerText = objRequest.responseText;//IE//document.getElementById("txtEchoOutPut").innerHTML = objRequest.responseText;//另外的浏览器}else{ document.getElementById("txtEchoOutPut").innerHTML= "There was a problem retrievingdata:<br>" + objRequest.statusText;}}}

你可能感兴趣的:(Ajax)