window.addEventListener("loade",initall,false);
var xhr=false;/*一个XMLHttpRequest对象*/
function initall(){
document.getElementById("txtDemo").addEventListener("click",getNewFile,false);
}
function getNewFile(event){/*获取文件的信息*/
makeRequest(this.href);
event.preventDefault();
}
function makeRequest(url){
/*兼容大多数浏览器*/
if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}
else{
if(window.ActiveXObject){
try {
xhr=new ActiveXObject("Microsoft.XMLHTTP")
} catch (error) {
}
}
}
if(xhr){
xhr.addEventListener("readystatechange",showContents,false);
xhr.open("GET",url,true);
xhr.send(null);
}
else{
document.getElementById("updateArea").innerHTML="Sorry";
}
}
function showContents(){
if(xhr.readyState==4){
if(xhr.status==200){
if(xhr.responseXML&&xhr.responseXML.childNodes.length>0){
var outMsg=getText(xhr.responseXML.getElementByTagName('choices')[0]);/*对于xml格式的文件需要有choices标签*/
}
else{
var outMsg=xhr.responseText;/*如果是txt文件,就直接返回这个文件的内容*/
}
}
else{
var outMsg="There is some problem with the request"+xhr.status;
}
document.getElementById("updateArea").innerHTML=outMsg;
}
function getText(inval){
if(inval.textContent){/*检测这个有没有textContent属性*/
return inval.textContent;
}
return inval.text;
}
}