1.不同的浏览器 创建 XMLHttpRequest 对象的方法(通用函数)
<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.一个 简单的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();"
/>
时间: <input type="text" name="time" />
</form>
</body>
</html>
这是 "time.asp" 的代码:<%
response.expires=-1 //页面不缓存 response.write(time)%>