K_Reverter的网页开发记录

要么不做,要么就当作艺术品来做!

导航

Google Maps API 2.0解析(15-GXslt和GXmlHttp实现AJAX)

        这次Google将AJAX的很多内容从Maps API之中分离出去,带一个独立的GAjax项目中去了,所以这些代码都是简单常规的代码。
        //GXml的静态方法value,返回指定XML节点的值
        function value(a)
        
{
            
if(!a)
            
{
                
return""
            }

            
var b="";
            
if(a.nodeType==3||a.nodeType==4||a.nodeType==2)
            
{
                b
+=a.nodeValue
            }

            
else if(a.nodeType==1||a.nodeType==9||a.nodeType==11)
            
{
                
for(var c=0;c<a.childNodes.length;++c)
                
{
                    b
+=arguments.callee(a.childNodes[c])
                }

            }

            
return b
        }

        
//GXmlHttp的静态函数create,返回一个xmlhttp对象
        function create()
        
{
            
try
            
{
                
if(typeof ActiveXObject!="undefined")
                
{
                    
return new ActiveXObject("Microsoft.XMLHTTP")
                }

                
else if(window.XMLHttpRequest)
                
{
                    
return new XMLHttpRequest()
                }

            }

            
catch(a)
            
{
            }

            
return null
        }

        
//下载指定的xml文档
        function GDownloadUrl(url,onload,usePost,contentType)
        
{
            
var xmlhttp=create();//获取一个xmlhttp对象
            if(!xmlhttp)return false;
            xmlhttp.onreadystatechange
=function()
            
{
                
if(xmlhttp.readyState==4)
                
{
                    onload(xmlhttp.responseText,xmlhttp.status);
                    xmlhttp.onreadystatechange
=nullFunction
                }

            }
;
            
if(usePost)
            
{
                xmlhttp.open(
"POST",url,true);
                
var f=contentType;
                
if(!f)
                
{
                    f
="application/x-www-form-urlencoded"
                }

                e.setRequestHeader(
"Content-Type",f);
                e.send(usePost)
            }

            
else
            
{
                e.open(
"GET",url,true);
                e.send(
null)
            }

            
return true
        }

        
function nullFunction()
        
{
        }
;
        
//GXml的静态方法parse,参数a是xml字符串
        function parse(a)
        
{
            
if(typeof ActiveXObject!="undefined"&&typeof GetObject!="undefined")//IE模式
            {
                
var b=new ActiveXObject("Microsoft.XMLDOM");
                b.loadXML(a);
                
return b
            }

            
if(typeof DOMParser!="undefined")//Firefox模式
            {
                
return(new DOMParser()).parseFromString(a,"text/xml")
            }

            
return CreateElement("div",null)
        }

        
//进行XSLT转化的类,构造函数是一个XSLT的xmlNode
        function GXslt(a)
        
{
            
this.xsltNode=a
        }

        GXslt.prototype.transformToHtml
=function(a,b)
        
{
            
if(a.transformNode)//浏览器是否支持XSLT转化(IE模式)
            {
                setInnerHTML(b,a.transformNode(
this.xsltNode));
                
return true
            }

            
else if(XSLTProcessor&&XSLTProcessor.prototype.jf)//(FireFox模式的XSLT转化,这一段我也不是太懂)
            {
                
var c=new XSLTProcessor();
                c.jf(
this.mh);//jf究竟是一个什么方法呢?浏览器集成的,还是Google的AJAX里面提供的?
                var d=c.transformToFragment(a,window.document);
                removeNodeChildren(b);
                b.appendChild(d);
                
return true
            }

            
else
            
{
                
return false
            }

        }
;

posted on 2006-08-18 17:06  K_Reverter  阅读(1845)  评论(1编辑  收藏  举报