知识大全 给大家讲个不错的ajax组件

Posted

篇首语:莫问天涯路几重,轻衫侧帽且从容。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 给大家讲个不错的ajax组件相关的知识,希望对你有一定的参考价值。

  function Xajax()        if (xajaxDebug) this DebugMessage = function(text) alert( Xajax Debug:\\n + text) ;

  this workId = xajaxWork + new Date() getTime();    this depth = ;

  //Get the XMLHttpRequest Object    this getRequestObject = function()        if (xajaxDebug) this DebugMessage( Initializing Request Object );    var req;    try        req=new ActiveXObject( Msxml XMLHTTP );        catch (e)        try        req=new ActiveXObject( Microsoft XMLHTTP );        catch (e )        req=null;            if(!req && typeof XMLHttpRequest != undefined )    req = new XMLHttpRequest();

  if (xajaxDebug)     if (!req) this DebugMessage( Request Object Instantiation failed );   

  return req;   

  // xajax $() is shorthand for document getElementById()    this $ = function(sId)//$是什么?        return document getElementById(sId);   

  // xajax include(sFileName) dynamically includes an external javascript file    this include = function(sFileName)        var objHead = document getElementsByTagName( head );    var objScript = document createElement( script );    objScript type = text/javascript ;    objScript src = sFileName;    objHead[ ] appendChild(objScript);   

  // xajax addHandler adds an event handler to an element    this addHandler = function(sElementId sEvent sFunctionName)        if (window addEventListener)        //eval的含义?    eval( this $( +sElementId+ ) addEventListener( +sEvent+ +sFunctionName+ false); );        else        eval( this $( +sElementId+ ) attachEvent( on +sEvent+ +sFunctionName+ fal**); );       

  // *ajax removeHandler removes an event handler from an element    this removeHandler = function(sElementId sEvent sFunctionName)        if (window addEventListener)        eval( this $( +sElementId+ ) removeEventListener( +sEvent+ +sFunctionName+ false); );        else        eval( this $( +sElementId+ ) detachEvent( on +sEvent+ +sFunctionName+ fal**); );       

  // *ajax create creates a new child node under a parent    this create = function(sParentId sTag sId)        var objParent = this $(sParentId);    objElement = document createElement(sTag);    objElement setAttribute( id sId);    objParent appendChild(objElement);   

  // xajax insert inserts a new node before another node    this insert = function(sBeforeId sTag sId)        var objSibling = this $(sBeforeId);    objElement = document createElement(sTag);    objElement setAttribute( id sId);    objSibling parentNode insertBefore(objElement objSibling);   

  this getInput = function(sType sName sId)        var Obj;    if (sType == radio && !window addEventListener)        alert( here );    Obj = document createElement( <input type= radio id= +sId+ name= +sName+ > );        else        Obj = document createElement( input );    Obj setAttribute( type sType);    Obj setAttribute( name sName);    Obj setAttribute( id sId);        return Obj;   

  // xajax createInput creates a new input node under a parent    this createInput = function(sParentId sType sName sId)        var objParent = this $(sParentId);    var objElement = this getInput(sType sName sId);    objParent appendChild(objElement);   

  // xajax insertInput creates a new input node before another node    this insertInput = function(sBeforeId sType sName sId)        var objSibling = this $(sBeforeId);    var objElement = this getInput(sType sName sId);    objSibling parentNode insertBefore(objElement objSibling);   

  // xajax remove deletes an element    this remove = function(sId)        objElement = this $(sId);    if (objElement parentNode && objElement parentNode removeChild)        objElement parentNode removeChild(objElement);       

  //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)> )        var newTxt = ;    while (txt indexOf(sSearch) > )        x = txt indexOf(sSearch)+sSearch length+ ;    newTxt += txt substr( 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

  function Xajax()        if (xajaxDebug) this DebugMessage = function(text) alert( Xajax Debug:\\n + text) ;

  this workId = xajaxWork + new Date() getTime();    this depth = ;

  //Get the XMLHttpRequest Object    this getRequestObject = function()        if (xajaxDebug) this DebugMessage( Initializing Request Object );    var req;    try        req=new ActiveXObject( Msxml XMLHTTP );        catch (e)        try        req=new ActiveXObject( Microsoft XMLHTTP );        catch (e )        req=null;            if(!req && typeof XMLHttpRequest != undefined )    req = new XMLHttpRequest();

  if (xajaxDebug)     if (!req) this DebugMessage( Request Object Instantiation failed );   

  return req;   

  // xajax $() is shorthand for document getElementById()    this $ = function(sId)//$是什么?        return document getElementById(sId);   

  // xajax include(sFileName) dynamically includes an external javascript file    this include = function(sFileName)        var objHead = document getElementsByTagName( head );    var objScript = document createElement( script );    objScript type = text/javascript ;    objScript src = sFileName;    objHead[ ] appendChild(objScript);   

  // xajax addHandler adds an event handler to an element    this addHandler = function(sElementId sEvent sFunctionName)        if (window addEventListener)        //eval的含义?    eval( this $( +sElementId+ ) addEventListener( +sEvent+ +sFunctionName+ false); );        else        eval( this $( +sElementId+ ) attachEvent( on +sEvent+ +sFunctionName+ fal**); );       

  // *ajax removeHandler removes an event handler from an element    this removeHandler = function(sElementId sEvent sFunctionName)        if (window addEventListener)        eval( this $( +sElementId+ ) removeEventListener( +sEvent+ +sFunctionName+ false); );        else        eval( this $( +sElementId+ ) detachEvent( on +sEvent+ +sFunctionName+ fal**); );       

  // *ajax create creates a new child node under a parent    this create = function(sParentId sTag sId)        var objParent = this $(sParentId);    objElement = document createElement(sTag);    objElement setAttribute( id sId);    objParent appendChild(objElement);   

  // xajax insert inserts a new node before another node    this insert = function(sBeforeId sTag sId)        var objSibling = this $(sBeforeId);    objElement = document createElement(sTag);    objElement setAttribute( id sId);    objSibling parentNode insertBefore(objElement objSibling);   

  this getInput = function(sType sName sId)        var Obj;    if (sType == radio && !window addEventListener)        alert( here );    Obj = document createElement( <input type= radio id= +sId+ name= +sName+ > );        else        Obj = document createElement( input );    Obj setAttribute( type sType);    Obj setAttribute( name sName);    Obj setAttribute( id sId);        return Obj;   

  // xajax createInput creates a new input node under a parent    this createInput = function(sParentId sType sName sId)        var objParent = this $(sParentId);    var objElement = this getInput(sType sName sId);    objParent appendChild(objElement);   

  // xajax insertInput creates a new input node before another node    this insertInput = function(sBeforeId sType sName sId)        var objSibling = this $(sBeforeId);    var objElement = this getInput(sType sName sId);    objSibling parentNode insertBefore(objElement objSibling);   

  // xajax remove deletes an element    this remove = function(sId)        objElement = this $(sId);    if (objElement parentNode && objElement parentNode removeChild)        objElement parentNode removeChild(objElement);       

  //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)> )        var newTxt = ;    while (txt indexOf(sSearch) > )        x = txt indexOf(sSearch)+sSearch length+ ;    newTxt += txt substr( 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( ? )== ? ?xajax= +encodeURIComponent(sFunction): &xajax= +encodeURIComponent(sFunction);    for (i = ; 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 = ; 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/ );    r setRequestHeader( Content Type application/x 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 != )    return;

  if (r status== )        if (xajaxDebug && r responseText length < ) xajax DebugMessage( Received:\\n + r responseText);    else if (xajaxDebug) xajax DebugMessage( Received:\\n + r responseText substr( )+ \\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;   

  //Process XML xajaxResponses returned from the request    this processResponse = function(xml)        if (xajaxStatusMessages == true) window status = Processing ;    var tmpXajax = null;    xml = xml documentElement;    if (xml == null)     alert( Error: the XML response that was returned from the server cannot be processed );    document body style cursor = default ;    if (xajaxStatusMessages == true) window status = XML response processing error ;    return;        for (i= ; i<xml childNodes length; i++)        if (xml childNodes[i] nodeName == cmd )        var cmd;    var id;    var property;    var data;    var search;    var type;    var before;

  for (j= ; j<xml childNodes[i] attributes length; j++)        if (xml childNodes[i] attributes[j] name == n )        cmd = xml childNodes[i] attributes[j] value;        if (xml childNodes[i] attributes[j] name == t )        id = xml childNodes[i] attributes[j] value;        if (xml childNodes[i] attributes[j] name == p )        property = xml childNodes[i] attributes[j] value;        if (xml childNodes[i] attributes[j] name == c )        type = xml childNodes[i] attributes[j] value;            if (xml childNodes[i] childNodes length > )        for (j= ; j<xml childNodes[i] childNodes length; j++)        if (xml childNodes[i] childNodes[j] nodeName == s )        if (xml childNodes[i] childNodes[j] firstChild)    search = xml childNodes[i] childNodes[j] firstChild nodeValue;        if (xml childNodes[i] childNodes[j] nodeName == r )        if (xml childNodes[i] childNodes[j] firstChild)    data = xml childNodes[i] childNodes[j] firstChild data;                else if (xml childNodes[i] firstChild)    data = xml childNodes[i] firstChild nodeValue;    else    data = ;

  var objElement = this $(id);    try        if (cmd== al )        alert(data);        if (cmd== js )        eval(data);        if (cmd== in )        this include(data);        if (cmd== as )        if (this willChange(id property data))        eval( objElement +property+ =data; );            if (cmd== ap )        eval( objElement +property+ +=data; );        if (cm*== ** )        eval( objElement +property+ =data+objElement +property);        if (cmd== rp )        this replace(id property search data)        if (cmd== rm )        this remove(id);        if (cmd== ce )        this create(id data property);        if (cmd== ie )        this insert(id data property);        if (cmd== ci )        this createInput(id type data property);        if (cmd== ii )        this insertInput(id type data property);        if (cmd== ev )        eval( this $( +id+ ) +property+ = function() +data+ ; );        if (cmd== ah )        this addHandler(id property data);        if (cmd== rh )        this removeHandler(id property data);            catch(e)        alert(e);        delete objElement;    delete cmd;    delete id;    delete property;    delete search;    delete data;    delete type;    delete before;            delete xml;    document body style cursor = default ;    if (xajaxStatusMessages == true) window status = Done ;       

cha138/Article/program/Java/hx/201311/25917

相关参考

知识大全 ajax中如何解决中文乱码

  在action中加入responsesetContentType(text/;charset=GBK);来设置编码格式就可以了  因为  使用ajax在发出请求后xml组件在解析返回数据的编码时是

知识大全 要我讲个笑话

面试成功与否往往决定能否得到工作。乔军伟摄大学生求职面试遭遇囧题文/记者黄茜实习生武威自以为准备充足,面对考官提问一定能对答如流;没想到「实战」时却遇到「囧题」,不知怎么答才好。人力资源专家表示,「囧

知识大全 听说上海商业金融学院今年招的生源不错,有在校的新生吗,可以给我说一下啊,谢谢大家了

听说上海商业金融学院今年招的生源不错,有在校的新生吗,可以给我说一下啊,谢谢大家了?你是今年的高考落榜生吗,准备自考了是吧。目前自考入学门槛比较低,对于分数没有太高的限制,所以你问落榜生自考要什么条件

知识大全 ajax入门小程序

  此小程序主要给ajax还未入门的朋友们一个简单的演示程序的主要内容就是将页面部分的id通过表单传到java的servlet中因为只是做示例所以没有对id进行任何处理有兴趣的朋友可以连接数据库或应用

知识大全 c#下简单的Ajax例子——Ajax.dll

c#下简单的Ajax例子——Ajax.dll  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  小研

知识大全 Ajax简介

PHP网络开发详解:Ajax简介  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &nbs

知识大全 ajax调用WCF服务

ajax调用WCF服务  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  关于AJAX调用WCF服务

知识大全 asp.net中mvc使用ajax提交参数的匹配问题解决探讨

本文为大家介绍下使用javaScript解决aspnet中mvc使用ajax提交参数的匹配问题遇到类似情况的朋友可以参考下希望对大家有所帮助 想到在aspnet的mvc中如果使用ajax向服

知识大全 Ajax+PHP简单入门[1]

Ajax+PHP简单入门[1]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Ajax由HTML

知识大全 怎样成为AJAX高手

怎样成为AJAX高手?  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  提起JavaScript很