<!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>Ajax</title>
</head>
<body>
<div id="new" ></div>
<script>
function getHTTPObject(){
if(typeof XMLHttpRequest=="undefined")
XMLHttpRequest=function(){
try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}
catch(e){}
try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}
catch(e){}
try{return new ActiveXObjec("Msxml2.XMLHTTP");}
catch(e){}
try{return new ActiveXObject("Microsoft.XMLHTTP");}
catch(e){}
return false;
}
return new XMLHttpRequest();
}
function getNewContent(){
var request=getHTTPObject();
if(request){
request.open("GET","example.txt",true);
request.onreadystatechange=function(){
if(request.readyState==4){
var para= document.createElement("p");
var txt=document.createTextNode(request.responseText);
para.appendChild(txt);
document.getElementById('new').appendChild(para);
}
};
request.send(null);
}else{
alert('Sorry,your browser dont\'t support XMLhttpRequest');
}
alert("Function Done")
}
function addLoadEvent(func){
var onload=window.onload;
if(typeof (window.onload) !='function'){
window.onload=func;
}else{
window.onload=function(){
onload();
func();
}
}
}
addLoadEvent(getNewContent);
addLoadEvent(getHTTPObject);
</script>
</body>
</html>
文本和这个html在一个文件夹下,txt内容:你好!
Elem
杰杰就是我
相关分类