1.不同的瀏覽器創(chuàng)建 XMLHttpRequest 對象的方法(通用函數(shù))
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp
;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
//IE 6.0+
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
//IE 5.5+
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的瀏覽器不支持AJAX!");
return false;
}
}
}
}
</script>
2.一個(gè)簡單的Ajax程序范例
<html>
<body>
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp
;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的瀏覽器不支持AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange
=function()
{
//4代表請求已完成
if(xmlHttp.readyState
==4)
{
document.myForm.time.value=xmlHttp.responseText
;
}
}
xmlHttp.open("GET","time.asp",true)
;
xmlHttp.send(null)
;
}
</script>
<form name="myForm">
用戶: <input type="text" name="username" onkeyup
="ajaxFunction();"
/>
時(shí)間: <input type="text" name="time" />
</form>
</body>
</html>
這是 "time.asp" 的代碼:
<%
response.expires=-1 //頁面不緩存
response.write(time)
%>
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報(bào)。