23
Jun 2004

XmlHttpRequest Object

用了Gmail的都发现,很多操作gmail是不用提交页面的,比如s(star),当你用x选中一个conversion,按下s要标记为star的时候,google根本没有提交整个页面就完成了这项操作,这大大改善了基于Web的用户界面的友好程度。

这里实现的秘密就是Xml Http Request对象。

在gmail的主页面中,有这样两个frameset:

<frameset rows='100%,*' border=0> <frame name=main src=/gmail/html/loading.html frameborder=0 noresize scrolling=no> <frame name=js src=/gmail?view=page&name=js&ver=7e0ccc547f92bc35 frameborder=0 noresize> </frameset>

第二个frameset名为js,高度为0,可想而知,这应该是这个页面背后使用的所有js脚本。这个脚本下载后发现有250KB之多。这里面有这样一个函数:

    function XmlHttpCreate(){
    var xmlhttp=null;
    if(is_ie){
        var control=(is_ie5)?"Microsoft.XMLHTTP":"Msxml2.XMLHTTP";
        try{
            xmlhttp=new ActiveXObject(control);
        }catch(e){
            DumpException(e);
            alert("You need to enable active scripting and activeX controls.");
        }
    }else {
        xmlhttp=new XMLHttpRequest();
        if(!xmlhttp){
            alert("XMLHttpRequest is not supported on this browser.");
        }
    }
    return xmlhttp;
}
啊,就在这里了。这就是有名的Xml HTTP Request对象了,这时候我才发现,这个对象不是gmail自己实现的,而是浏览器原本就支持的。google for it,找到了这样一个页面 Using the XML HTTP Request object 原来这东西2002年就有了,我可真是孤陋啊。

comments powered by Disqus