Simplified Ajax encapsulation that imitates prototype

Source: Internet
Author: User

Prototype was used for projects some time ago. JS later found some complicated situations, sometimes with memory overflow, and simply needed it. To ensure program compatibility, it implemented a simple Ajax encapsulation based on the prototype interface, firefox is not tested. the number of lines to be compressed is not formatted.

 

<HTML>
<Head>
<SCRIPT>
VaR syj = {};
// Method for sending an Ajax request
Syj. Ajax = function (URL, OBJ ){
VaR httprequest;
If (window. activexobject) Try {httprequest = new activexobject ("Microsoft. XMLHTTP ");} catch (e) {try {httprequest = new activexobject (" msxml2.xmlhttp ");} catch (e ){}}
If (! Httprequest) {alert ('cannot create XMLHTTP instance'); obj. oncomplete ();}
Httprequest. onreadystatechange = function () {If (httprequest. readystate = 4) {OBJ ['oncomplete'] (httprequest );}}
If (URL. indexof ('machinedate') =-1) URL + = (URL. indexof ('? ') =-1? "? ":" & ") + (" Machinedate = "+ new date (). gettime ());
URL + = (URL. indexof ('? ') =-1? "? ":" & ") + Obj. parameters;
If (obj. asynchronous = true) httprequest. Open (obj. method, URL); else httprequest. Open (obj. method, URL, false );
Httprequest. Send (null );
Return httprequest;
};
Function Test (){
Alert ("Test start ");
VaR url = "http://www.baidu.com /";

Syj. Ajax (URL ,{
Method: "Get ",
Parameters: "s? WD = csdn ",
Asynchronous: True,
Onfailure: function (httprequest ){
Alert ("error! ");
},
Oncomplete: function (httprequest ){
If (httprequest. Status = 200 ){
Alert (httprequest. responsetext );
}
}
}
);
Alert ("test end ");
}
</SCRIPT>
</Head>
<Body>
<Input type = "button" onclick = "test ()" value = "test"/>
</Body>
</Html>

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.