XMLHttpRequest提供了一种简单的方式在不刷新页面的同时从URL中检索数据。网页可以只更新页面部分的内容。
function loadXMLDoc() {
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var item = xmlhttp.responseXML;
var User = item.getElementsByTagName("User")[0].attributes['0'].value;
var Pwd = item.getElementsByTagName("Pwd")[0].attributes['0'].value;
document.getElementById("myDiv").innerHTML = User + "\r" + Pwd;
}
}
xmlhttp.open("GET", "..\\Db\\xmlhttp_info.xml", true);
xmlhttp.send();
}
XML
<root>
<User value="Edward">
Edward
</User>
<Pwd value="123">
123
</Pwd>
</root>
Reference: