首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
中国IT教育
Google
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME Java&XML 开源技术 其他技术 RSS订阅 论坛 专题
您现在的位置: 中国IT实验室 >> Java >> 核心技术 >> 高级编程 >> 正文

给大家讲个不错的ajax组件

    //xajax.replace searches for text in an attribute of an element and replaces it
    //with a different text
    this.replace = function(sId,sAttribute,sSearch,sReplace)
    {
    var bFunction = false;

    eval("var txt=document.getElementById('"+sId+"')."+sAttribute);
    if (typeof txt == "function")
            {
                txt = txt.toString();
                bFunction = true;
            }
    if (txt.indexOf(sSearch)>-1)
    {
    var newTxt = '';
    while (txt.indexOf(sSearch) > -1)
    {
    x = txt.indexOf(sSearch)+sSearch.length+1;
    newTxt += txt.substr(0,x).replace(sSearch,sReplace);
    txt = txt.substr(x,txt.length-x);
    }
    newTxt += txt;
    if (bFunction)
    {
    eval("newTxt =" + newTxt);
    eval('this.$("'+sId+'").'+sAttribute+'=newTxt;');
    }
    else if (this.willChange(sId,sAttribute,newTxt))
    {
    eval('this.$("'+sId+'").'+sAttribute+'=newTxt;');
    }
    }
    }

    // xajax.getFormValues() builds a query string XML message from the elements of a form object

 

    this.call = function(sFunction, aArgs, sRequestType)
    {
    var i,r,postData;
    if (document.body && xajaxWaitCursor)
    document.body.style.cursor = 'wait';
    if (xajaxStatusMessages == true) window.status = 'Sending Request...';
    if (xajaxDebug) this.DebugMessage("Starting xajax...");
    if (sRequestType == null) {
       var xajaxRequestType = xajaxDefinedPost;
    }
    else {
    var xajaxRequestType = sRequestType;
    }
    var uri = xajaxRequestUri;
    var value;
    switch(xajaxRequestType)
    {
    ca** *ajaxDefinedGet:{
    var uriGet = uri.indexOf("?")==-1?"?xajax="+encodeURIComponent(sFunction):"&xajax="+encodeURIComponent(sFunction);
    for (i = 0; i<aArgs.length; i++)
    {
    value = aArgs[i];
    if (typeof(value)=="object")
    value = this.objectToXML(value);
    uriGet += "&xajaxargs[]="+encodeURIComponent(value);
    }
    uriGet += "&xajaxr=" + new Date().getTime();
    uri += uriGet;
    postData = null;
    } break;
    ca** *ajaxDefinedPost:{
    postData = "xajax="+encodeURIComponent(sFunction);
    postData += "&xajaxr="+new Date().getTime();
    for (i = 0; i <aArgs.length; i++)
    {
    value = aArgs[i];
    if (typeof(value)=="object")
    value = this.objectToXML(value);
    postData = postData+"&xajaxargs[]="+encodeURIComponent(value);
    }
    } break;
    default:
    alert("Illegal request type: " + xajaxRequestType); return false; break;
    }
    r = this.getRequestObject();
    r.open(xajaxRequestType==xajaxDefinedGet?"GET":"POST", uri, true);
    if (xajaxRequestType == xajaxDefinedPost)
    {
    try
    {
    r.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
    r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=" + xajaxCharEncoding);
    }
    catch(e)
    {
    alert("Your browser does not appear to  support asynchronous requests using POST.");
    return false;
    }
    }
    r.onreadystatechange = function()
    {
    if (r.readyState != 4)
    return;

    if (r.status==200)
    {
    if (xajaxDebug && r.responseText.length < 1000) xajax.DebugMessage("Received:\n" + r.responseText);
    else if (xajaxDebug) xajax.DebugMessage("Received:\n" + r.responseText.substr(0,1000)+"...\n[long response]\n...</xajax>");
    if (r.respon***ML)
    xajax.processResponse(r.respon***ML);
    else {
    alert("Error: the XML response that was returned from the server is invalid.");
    document.body.style.cursor = 'default';
    if (xajaxStatusMessages == true) window.status = 'Invalid XML response error';
    }
    }

    delete r;
    }
    if (xajaxDebug) this.DebugMessage("Calling "+sFunction +" uri="+uri+" (post:"+ postData +")");
    r.send(postData);
    if (xajaxStatusMessages == true) window.status = 'Waiting for data...';
    delete r;
    return true;
    }

    // Tests if the new Data is the same as the extant data
    this.willChange = function(element, attribute, newData)
    {
    if (!document.body)
    {
    return true;
    }
    var oldData;
    if (attribute == "innerHTML")
    {
    tmpXajax = this.$(this.workId);
    if (tmpXajax == null)
    {
    tmpXajax = document.createElement("div");
    tmpXajax.setAttribute('id',this.workId);
    tmpXajax.style.display = "none";
    tmpXajax.style.visibility = "hidden";
    document.body.appendChild(tmpXajax);
    }
    tmpXajax.innerHTML = newData;
    newData = tmpXajax.innerHTML;
    tmpXajax.innerHTML = '';
    }
    eval("oldData=document.getElementById('"+element+"')."+attribute);
    if (newData != oldData)
    return true;

    return false;
    }

上一页  [1] [2] [3] [4] 下一页

【责编:Ken】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 ·Java入门 Tomcat的配置技巧精华专题…
 ·Oracle RMAN物理备份技术详解…
 今日更新
 社区讨论
 博客论点
 频道精选
 Java 频道导航