Chinaunix首页 | 论坛 | 博客
  • 博客访问: 366562
  • 博文数量: 284
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1707
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-14 16:38
文章分类

全部博文(284)

文章存档

2015年(6)

2014年(278)

我的朋友

分类: JavaScript

2014-07-08 15:56:33

1. [代码][JavaScript]代码     
 //javascript Object: ajax Object
//Created By RexLee
function Ajax(url,data){
    this.url=url;
    this.data=data;
    this.browser=(function(){  
        if(navigator.userAgent.indexOf("MSIE")>0) {  
            return "MSIE";//IE浏览器
        }else{
            return "other";//其他
    }})();
};
Ajax.prototype={
    get:function(){
        var result;
        var xmlhttp;
        if(this.browser=='MSIE'){
            try{
                xmlhttp=new ActiveXObject('microsoft.xmlhttp');
                }catch(e){
                    xmlhttp=new ActiveXObject('msxml2.xmlhttp');
                    }
            }else{
                xmlhttp=new XMLHttpRequest();
            };
        xmlhttp.onreadystatechange=function(){
            result = xmlhttp.responseText;//闭包,不能采用this.属性
        };
        xmlhttp.open('GET',this.url+'?'+this.data,false);//true无法抓取数据,why?
        xmlhttp.send(null);
        return result;
    },
    post:function(){
        var result;
        var xmlhttp;
        if(this.browser=='MSIE'){
            xmlhttp=new ActiveXObject('microsoft.xmlhttp');
            }else{
                xmlhttp=new XMLHttpRequest();
            };
        xmlhttp.onreadystatechange=function(){
            result = xmlhttp.responseText;//闭包,不能采用this.属性
        };
        xmlhttp.open('POST',this.url,false);//需设为false,否则无法抓取responseText
        xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//POST中,这句必须
        xmlhttp.send(this.data);
        return result;
    }
};
 
//var a=new ajax('opp2.js','');
//alert('by GET\\n'+a.get())
//alert('by POST\\n'+a.post());
///////////////////////////////
 
 
 
window.onload=function(){
document.getElementById("btn").onclick=function(){
    var p=document.getElementById("t").value;
    var a=new Ajax("phpOOP/getPage.php","page="+p);
    document.getElementById("box").innerHTML=a.get();
    };
}
阅读(381) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~