ajax浏览器兼容的问题一直都是同学们多烦恼的,下面有个不错的示例大家可以参考下,希望对大家有所帮助
代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script>window.onload = function(){var oBtn = document.getElementById('btn');oBtn.onclick = function(){//1.创建ajax对象//只支持非IE6浏览器var oAjax = null;if(window.XMLHttpRequest){oAjax = new XMLHttpRequest();//alert(new XMLHttpRequest());}else{//只支持IE6浏览器oAjax = new ActiveXObject("Microsoft.XMLHTTP");}//2.连接服务器,这里加个时间参数,每次访问地址都不一样,浏览器就不用浏览器里的缓冲了,但// 但服务器那端是不解析这个时间的oAjax.open("get","a.txt?t=" + new Date().getTime(),true);//3.发送oAjax.send(null);//4.接受信息oAjax.onreadystatechange = function(){//浏览器与服务器之间的交互,进行到哪一步了,当等于4的时候,代表读取完成了if(oAjax.readyState==4){//状态码,只有等于200,代表接受完成,并且成功了if(oAjax.status==200){alert("成功" + oAjax.responseText);}else{alert("失败");}}};};};</script></head><body><input type="button" value="按钮" id="btn"/></body></html>