Self-encapsulated Ajax-javascript

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.