Ajax encapsulation code

Source: Internet
Author: User

Ajax EncapsulationCode
VaR waitelement;
VaR scrollx, scrolly =-1;
Function Ajax (){
/// Create an object
VaR XMLHTTP;
Try
{
If (window. activexobject ){
/* Do not delete the following comments, which are not comments */
/* @ Cc_on @*/
/* @ If (@ _ jscript_version> = 5)
Try {
XMLHTTP = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){
XMLHTTP = false;
}
}
@ End @*/
} Else {
XMLHTTP = new XMLHttpRequest ();
}
If (! XMLHTTP & typeof XMLHttpRequest! = 'Undefined '){
XMLHTTP = new XMLHttpRequest ();
}
}
Catch (E)
{
Alert (E. Message );
}

If (! XMLHTTP)
{
Alert ("your browser does not support XMLHTTP objects, so some features cannot be used. We recommend that you use a later version of the browser !! ");
Return;
}
/// Function topic
This. getdata = function (URL, Data, dodata)
{
VaR verbs = data;
// Alert (data );
XMLHTTP. Open ("Post", URL, true );

XMLHTTP. onreadystatechange = function (){
If (XMLHTTP. readystate = 4 ){
Waitelement. style. Visibility = 'hiden ';
Dodata (XMLHTTP );

}
Else {
Createwaitelement ();
Waitelement. style. Visibility = 'visable ';
Movewaitelement ();
}
}
XMLHTTP. setRequestHeader ("Content-Length", verbs. Length );
XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
XMLHTTP. Send (verbs );
}
/////
This. geturldata = function (URL, Data, dodata)
{
XMLHTTP. Open ("get", URL, true );

XMLHTTP. onreadystatechange = function (){

If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200 ){
Dodata (XMLHTTP );
}
Else
{
Waitelement. style. Visibility = 'visable ';
}

}
XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
XMLHTTP. Send (null );
}
This. getformdata = function (DEMO)
{
// Alert (DEMO );
}
/// Function topic
}

If (window. addeventlistener ){
Window. addeventlistener ('scroll ', movewaitelement, false );
Window. addeventlistener ('resize', movewaitelement, false );
}
Else if (window. attachevent ){
Window. attachevent ('onscroll ', movewaitelement );
Window. attachevent ('onresize', movewaitelement );
}

Function movewaitelement (){
VaR scrollyt, scrollxt;
If (! Waitelement)
Createwaitelement ();
If (typeof (window. pageyoffset) = "Number "){
Scrollyt = Window. pageyoffset;
Scrollxt = Window. pagexoffset;
}
Else if (document. Body & document.doc umentelement. scrolltop ){
Scrollyt = document.doc umentelement. scrolltop;
Scrollxt = Document. Body. scrollleft;
}
Else if (document. Body & typeof (document. Body. scrolltop) = "Number "){
Scrollyt = Document. Body. scrolltop;
Scrollxt = Document. Body. scrollleft;
}
If (scrollx! = Scrollxt | scrolly! = Scrollyt ){
Scrollx = scrollxt;
Scrolly = scrollyt;
VaR width = Document. Body. clientwidth;
Waitelement. style. Top = scrollyt + "PX ";
Waitelement. style. Right =-scrollxt + "PX ";
}
}
Function createwaitelement (){
VaR ELEM = Document. getelementbyid ('_ ajax_loading ');
If (! ELEM ){
ELEM = Document. createelement ("Div ");
ELEM. ID = '_ ajax_loading ';
ELEM. style. Position = 'absolute ';
ELEM. style. Height = 17;
ELEM. style. paddingleft = "3px ";
ELEM. style. paddingright = "3px ";
ELEM. style. fontsize = "11px ";
ELEM. style. fontfamily = 'arial, verdana, tahoma ';
ELEM. style. Border = "#000000 1px solid ";
ELEM. style. backgroundcolor = "red ";
ELEM. style. color = "# ffffff ";
ELEM. innerhtml = 'loading ...';
ELEM. style. Visibility = 'hiden ';
Document. Body. insertbefore (ELEM, document. Body. firstchild );
}
Waitelement = ELEM;
}

 

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.