28,406
社区成员
发帖
与我相关
我的任务
分享
<script language="javascript" src="ajax.js"></script>
<div id="ajax_content"></div>
<input type="button" onclick="getsubcategory('http://www.baidu.com?s=1','ajax_content');" value="测试">
function createxmlhttp()
{
xmlhttpobj = false;
try{//创建对象
xmlhttpobj = new XMLHttpRequest;
}catch(e){
try{
xmlhttpobj=new ActiveXObject("MSXML2.XMLHTTP");
}catch(e2){
try{
xmlhttpobj=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e3){
xmlhttpobj = false;
}
}
}
return xmlhttpobj;
}
function getsubcategory(url_str,ajax_to_id){
//alert(url_str);
if(url_str==""){
document.getElementById(ajax_to_id).innerHTML="url参数有误";
return;
};
var xmlhttpobj = createxmlhttp();
if(xmlhttpobj){//如果创建对象xmlhttpobj成功
//alert(url_str+"&number="+Math.random());
//return;
xmlhttpobj.open('get',url_str+"&rnd_number="+Math.random(),true);//get方法 加个随机数。
xmlhttpobj.send(null);
xmlhttpobj.onreadystatechange=function(){//客户端监控函数
//加载等待提示信息
load_str = "加载中…"
if(xmlhttpobj.readyState==4){//服务器处理请求完成
//干她娘的firefox浏览器,上面的readyState中的S必须大写,否则在firefox中不起作用,干她老母,这也区分大小写!
if(xmlhttpobj.status==200){
//alert('ok');
var html = xmlhttpobj.responseText;//获得返回值
document.getElementById(ajax_to_id).innerHTML=html;
}else{
document.getElementById(ajax_to_id).innerHTML="对不起,您请求的页面有问题...";
}
}else{
document.getElementById(ajax_to_id).innerHTML=load_str;//服务器处理中
}
}
}
}