首页 技术 正文
技术 2022年11月20日
0 收藏 319 点赞 4,650 浏览 1117 个字
 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
var xhr=initAjax();
function initAjax()
{
if(window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try
{
return new ActiveXObject("Msxml2.XMLHttp");
}
catch(e)
{
try
{
return new ActiveXObject("Microsoft.XMLHttp");
}
catch(e)
{
//return undefined
alert("Your browser doesn't support AJAX!");
}
}
}
}
function showInfo()
{
//var xhr=new ActiveXObject("Msxml2.XMLHttp");
if(xhr!=null)
{
xhr.onreadystatechange=xhrStateChange;
xhr.open("GET","info.xml",true);
xhr.send(null);
} }
function xhrStateChange()
{
if(xhr.readyState==4)
{
document.getElementById("infoDiv").innerHTML=xhr.responseText;
}
else if(xhr.readyState==3)
{
document.getElementById("infoDiv").innerHTML="Loading...";
} }
</script>
</head>
<body>
<input type="button" value="click me" onclick="showInfo()">
<div id="infoDiv"></div>
</body>
</html>

initAjax函数实现了对浏览器的兼容(IE5,6), 不过现在ActiveX技术早已过时, 被浏览器默认禁用.

showInfo函数中将xhrStateChange函数绑定到onreadystatechange事件, 注意这种绑定方式不支持传参(只写函数名不能加括号)!!!而且这句代码写在函数外也是可以的.结果如下

Ajax基本写法

第49行如果把innerHTML改为innerText, 则将不考虑xml文件中的标签, 内容原样显示, 如下

Ajax基本写法

另外, xhr除了responseText属性, 还有一个属性:responseXml.

附readyState的各状态

Ajax基本写法

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,839