百度空间 | 百度首页 
 
查看文章
 
Ajax框架
2009年06月19日 星期五 11:06

    if(!window.XMLHttpRequest)
    {
        window.XMLHttpRequest = function()
        {
            var progIDs = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
            for(var i = 0; i < progIDs.length(); i++)
            {
                try
                {
                    var request = new ActiveXObject(progIDs[i]);
                    return request;
                }
                catch(ex){}
            }
            return null;
        }
    }
   
    function DoNothing()
    {}
   
    function Ajax(callback, div, xkey)
    {
        var xhReq = new XMLHttpRequest();
        var ajax = this;
        this.httpRequest = xhReq;
        this.div = div;
        this.xkey = xkey;
        this.callback = callback;
        //If using 'xhReq.onreadystatechange', the result will be the same?
        this.httpRequest.onreadystatechange = function()
        {
            Ajax.OnReceiveResponse.call(ajax);
        }
       
        function startAnimation()
        {
            this.timerid = null;
            if(this.div.OnAjaxStart)
            {
                this.div.OnAjaxStart();
            }
            else
            {
                DoNothing();
            }
        }
       
        if(this.div)
        {
            this.timerid = SetTimeout(startAnimation, 500);
        }
    }
   
    Ajax.PengdingFragments = [];
   
    Ajax.HandlerUrl = function(url)
    {
        var time = new Date();
        return url + '&time=' + time.toString();
    }
   
    Ajax.FormEncode = function(query)
    {
        return '';
    }
   
    Ajax.Prototype.ConcurrenncyCheck = function()
    {
        return Ajax.DoConcurrencyCheck();
    }
   
    Ajax.DoConcurrencyCheck = function(xkey, div)
    {
        if(!xkey)
        {
            return true;
        }
       
        if(Ajax.PendingFragments.contains(xkey))
        {
            return false;
        }
       
        Ajax.PendingFragments.push(xkey);
        return true;
    }
   
    Ajax.OnReceiveResponse = function()
    {
        var xhReq = this.httpRequest;
        if(xhReq.readyState == 4)
        {
            if(this.timerid)
            {
                window.clearTimeout(this.timerid);
                this.timerid = null;
            }
           
            try
            {
                var response = xhReq.responseText;
                Ajax.PendingFragments.remove(this.xkey);
                switch(xhReq.status)
                {
                case 401:
                case 441:
                    //do something to handler these two status.
                    DoNothing();
                    return;
                case 200:
                    if (this.callback)
                    {
                        this.callback(response);
                    }
                }
            }
            finally
            {
                xhReq.onreadystatechange = function(){};
                this.httpRequest = null;
                this.xkey = null;
                this.callback = null;
                this.div = null;
            }
        }
    }
   
    Ajax.GetAsync = function(url, query, div, callback, xkey)
    {
        var ajax = new Ajax(callback, div, xkey);
        var xhReq = ajax.httpRequest;
        var seq = (/\?/.test(url)) ? '&' : '?';
        var fullUrl = url + sep + query;
        var fullUrl = url + sep + ((typeof query == 'string') ? query : Ajax.FormEncode(query));
        fullUrl = Ajax.HandlerUrl(fullUrl);   //add a querystring for each url to prevent cache's affect
       
        if(ajax.ConcurrencyCheck())
        {
            window.clearTimeout(ajax.timerid);
            ajax.timerid = null;
            return null;
        }
       
        xhReq.open("GET", fullUrl, true);
        xhReq.send(null);
        return ajax;
    }
   
    Ajax.PostAsync = function(url, body, div, callback, xkey)
    {
        var ajax = new Ajax(callback, div, xkey);
        var xhReq = ajax.httpRequest;
       
        if(ajax.ConcurrencyCheck())
        {
            window.clearTimeout(ajax.timerid);
            ajax.timerid = null;
            return null;
        }
       
        xhReq.open("POST", url, false);
        xhReq.send(body);
        return ajax;
    }
   
    Ajax.MakeRequest(url, div, body, activeObj, isPost)
    {
        function callback(result)
        {
            div.innerHTML = result;
        }
       
        if(isPost)
        {
            return Ajax.PostAysnc(url, body, div, callback, div);
        }
        else
        {
            return Ajax.GetAsync(url, body, div, callback, div);
        }
       
    }
   
    function GetHtmlInsideBody(html)
    {
        if(!/<html\b/.test(html))
        {
            return html;
        }
       
        var lines = html.split('\n');
        var insideBody = false;
        var html2 = '';
        for(var i = 0; i < lines.length; i++)
        {
            var line = lines[i];
            if(insidyBody)
            {
                if(/<\/body\b/.test(line))
                {
                    insideBody = false;
                }
                else
                {
                    html2 += line;
                }
            }
            else if(!insideBody && /<body\b/.test(line))
            {
                insideBody = true;
            }
        }
        return html2;
    }

暂且只有这么点,需要更多的优化。


类别:默认分类 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu