<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)
%>
本文介绍了如何在不同浏览器中创建XMLHttpRequest对象以实现Ajax功能,包括Firefox、Opera、Safari和Internet Explorer。提供了通用函数及简单示例,展示了在用户输入时通过Ajax获取服务器时间并更新到网页。
1613

被折叠的 条评论
为什么被折叠?



