Self-encapsulated Ajax-javascript
Demo: http://www.huiyi8.com/jiaoben/
From: http://www.huiyi8.com/sc/16885.html
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 browser
}else{
return "other";//Other
}})();
};
ajax.prototype={
Get:function () {
var result;
var xmlhttp;
if (this.browser== ' MSIE ') {AD code
try{
Xmlhttp=new ActiveXObject (' microsoft.xmlhttp ');
}catch (e) {
Xmlhttp=new ActiveXObject (' msxml2.xmlhttp ');
}
}else{
Xmlhttp=new XMLHttpRequest ();
};
Xmlhttp.onreadystatechange=function () {
result = xmlhttp.responsetext;//closure, cannot take this. property
};
Xmlhttp.open (' GET ', this.url+ '? ') +this.data,false);//true can't crawl data, 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;//closure, cannot take this. property
};
Xmlhttp.open (' POST ', this.url,false);//Must be set to false otherwise it cannot be crawled responsetext
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");//post, this sentence must be
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 ();
};
}
Self-encapsulated Ajax-javascript