• ajax封装类代码


    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;
    }

     

  • 相关阅读:
    C#编程规范
    实用JavaScript代码库
    JSON入门
    asp.net基本事件
    iOS开发笔记 3、iOS基础
    iOS开发笔记 8、真机调试和发布软件
    数据同步框架MS Sync Framework不同场景使用例子和简要分析
    使用AppFabric 承载WCF和WF服务实例分析和说明
    .net程序员的iPhone开发MonoTouch
    iOS开发笔记 4、iOS中的Cocoa、设计模式等
  • 原文地址:https://www.cnblogs.com/bestsaler/p/1835778.html
Copyright © 2020-2023  润新知