ajax封裝類代碼

來源:互聯網
上載者:User

ajax封裝類代碼
var waitElement;
var scrollX, scrollY = -1;
 function Ajax(){
    ////創造對象
    var xmlhttp;
    try
    {
        if (window.ActiveXObject){
            /* 不要刪除以下注釋,這部分不是注釋 */
            /*@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("你的瀏覽器不支援xmlhttp對象,所以一些功能無法使用,建議使用高版本的瀏覽器!!");
    return;
    }
    ///函數主題
        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='hidden'; 
                   dodata(xmlhttp); 
                        
                    }
                    else{
                    CreateWaitElement();                   
                   waitElement.style.visibility='visible';
                    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='visible';
            }
         
            }
             xmlhttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
            xmlhttp.send(null);
        }
        this.getFormData=function(demo)
        {
        //alert(demo);
        }
    ///函數主題
}

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.documentElement && document.documentElement.scrollTop) {
  scrollYT = document.documentElement.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 = '載入中 ...';
        elem.style.visibility = 'hidden';
        document.body.insertBefore(elem, document.body.firstChild);
    }
    waitElement = elem;
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.