熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

給大家講個不錯的ajax組件

2022-06-13   來源: Java核心技術 

  function Xajax()
    {
    if (xajaxDebug) thisDebugMessage = function(text) { alert(Xajax Debug:\n + text) };

  thisworkId = xajaxWork+ new Date()getTime();
    thisdepth = ;

  //Get the XMLHttpRequest Object
    thisgetRequestObject = function()
    {
    if (xajaxDebug) thisDebugMessage(Initializing Request Object);
    var req;
    try
    {
    req=new ActiveXObject(MsxmlXMLHTTP);
    }
    catch (e)
    {
    try
    {
    req=new ActiveXObject(MicrosoftXMLHTTP);
    }
    catch (e)
    {
    req=null;
    }
    }
    if(!req && typeof XMLHttpRequest != undefined)
    req = new XMLHttpRequest();

  if (xajaxDebug) {
    if (!req) thisDebugMessage(Request Object Instantiation failed);
    }

  return req;
    }

  // xajax$() is shorthand for documentgetElementById()
    this$ = function(sId)//$是什麼?
    {
    return documentgetElementById(sId);
    }

  // xajaxinclude(sFileName) dynamically includes an external javascript file
    thisinclude = function(sFileName)
    {
    var objHead = documentgetElementsByTagName(head);
    var objScript = documentcreateElement(script);
    objScripttype = text/javascript;
    objScriptsrc = sFileName;
    objHead[]appendChild(objScript);
    }

  // xajaxaddHandler adds an event handler to an element
    thisaddHandler = function(sElementId sEvent sFunctionName)
    {
    if (windowaddEventListener)
    {
    //eval的含義?
    eval(this$(+sElementId+)addEventListener(+sEvent++sFunctionName+false););
    }
    else
    {
    eval(this$(+sElementId+)attachEvent(on+sEvent++sFunctionName+fal**););
    }
    }

  // *ajaxremoveHandler removes an event handler from an element
    thisremoveHandler = function(sElementId sEvent sFunctionName)
    {
    if (windowaddEventListener)
    {
    eval(this$(+sElementId+)removeEventListener(+sEvent++sFunctionName+false););
    }
    else
    {
    eval(this$(+sElementId+)detachEvent(on+sEvent++sFunctionName+fal**););
    }
    }

  // *ajaxcreate creates a new child node under a parent
    thiscreate = function(sParentId sTag sId)
    {
    var objParent = this$(sParentId);
    objElement = documentcreateElement(sTag);
    objElementsetAttribute(idsId);
    objParentappendChild(objElement);
    }

  // xajaxinsert inserts a new node before another node
    thisinsert = function(sBeforeId sTag sId)
    {
    var objSibling = this$(sBeforeId);
    objElement = documentcreateElement(sTag);
    objElementsetAttribute(idsId);
    objSiblingparentNodeinsertBefore(objElement objSibling);
    }

  thisgetInput = function(sType sName sId)
    {
    var Obj;
    if (sType == radio && !windowaddEventListener)
    {
    alert(here);
    Obj = documentcreateElement(<input type=radio id=+sId+ name=+sName+>);
    }
    else
    {
    Obj = documentcreateElement(input);
    ObjsetAttribute(typesType);
    ObjsetAttribute(namesName);
    ObjsetAttribute(idsId);
    }
    return Obj;
    }

  // xajaxcreateInput creates a new input node under a parent
    thiscreateInput = function(sParentId sType sName sId)
    {
    var objParent = this$(sParentId);
    var objElement = thisgetInput(sType sName sId);
    objParentappendChild(objElement);
    }

  // xajaxinsertInput creates a new input node before another node
    thisinsertInput = function(sBeforeId sType sName sId)
    {
    var objSibling = this$(sBeforeId);
    var objElement = thisgetInput(sType sName sId);
    objSiblingparentNodeinsertBefore(objElement objSibling);
    }

  // xajaxremove deletes an element
    thisremove = function(sId)
    {
    objElement = this$(sId);
    if (objElementparentNode && objElementparentNoderemoveChild)
    {
    objElementparentNoderemoveChild(objElement);
    }
    }

  //xajaxreplace searches for text in an attribute of an element and replaces it
    //with a different text
    thisreplace = function(sIdsAttributesSearchsReplace)
    {
    var bFunction = false;

  eval(var txt=documentgetElementById(+sId+)+sAttribute);
    if (typeof txt == function)
            {
                txt = txttoString();
                bFunction = true;
            }
    if (txtindexOf(sSearch)>)
    {
    var newTxt = ;
    while (txtindexOf(sSearch) > )
    {
    x = txtindexOf(sSearch)+sSearchlength+;
    newTxt += txtsubstr(x)replace(sSearchsReplace);
    txt = txtsubstr(xtxtlengthx);
    }
    newTxt += txt;
    if (bFunction)
    {
    eval(newTxt = + newTxt);
    eval(this$(+sId+)+sAttribute+=newTxt;);
    }
    else if (thiswillChange(sIdsAttributenewTxt))
    {
    eval(this$(+sId+)+sAttribute+=newTxt;);
    }
    }
    }


    // xajaxgetFormValues() builds a query string XML message from the elements of a form object

  function Xajax()
    {
    if (xajaxDebug) thisDebugMessage = function(text) { alert(Xajax Debug:\n + text) };

  thisworkId = xajaxWork+ new Date()getTime();
    thisdepth = ;

  //Get the XMLHttpRequest Object
    thisgetRequestObject = function()
    {
    if (xajaxDebug) thisDebugMessage(Initializing Request Object);
    var req;
    try
    {
    req=new ActiveXObject(MsxmlXMLHTTP);
    }
    catch (e)
    {
    try
    {
    req=new ActiveXObject(MicrosoftXMLHTTP);
    }
    catch (e)
    {
    req=null;
    }
    }
    if(!req && typeof XMLHttpRequest != undefined)
    req = new XMLHttpRequest();

  if (xajaxDebug) {
    if (!req) thisDebugMessage(Request Object Instantiation failed);
    }

  return req;
    }

  // xajax$() is shorthand for documentgetElementById()
    this$ = function(sId)//$是什麼?
    {
    return documentgetElementById(sId);
    }

  // xajaxinclude(sFileName) dynamically includes an external javascript file
    thisinclude = function(sFileName)
    {
    var objHead = documentgetElementsByTagName(head);
    var objScript = documentcreateElement(script);
    objScripttype = text/javascript;
    objScriptsrc = sFileName;
    objHead[]appendChild(objScript);
    }

  // xajaxaddHandler adds an event handler to an element
    thisaddHandler = function(sElementId sEvent sFunctionName)
    {
    if (windowaddEventListener)
    {
    //eval的含義?
    eval(this$(+sElementId+)addEventListener(+sEvent++sFunctionName+false););
    }
    else
    {
    eval(this$(+sElementId+)attachEvent(on+sEvent++sFunctionName+fal**););
    }
    }

  // *ajaxremoveHandler removes an event handler from an element
    thisremoveHandler = function(sElementId sEvent sFunctionName)
    {
    if (windowaddEventListener)
    {
    eval(this$(+sElementId+)removeEventListener(+sEvent++sFunctionName+false););
    }
    else
    {
    eval(this$(+sElementId+)detachEvent(on+sEvent++sFunctionName+fal**););
    }
    }

  // *ajaxcreate creates a new child node under a parent
    thiscreate = function(sParentId sTag sId)
    {
    var objParent = this$(sParentId);
    objElement = documentcreateElement(sTag);
    objElementsetAttribute(idsId);
    objParentappendChild(objElement);
    }

  // xajaxinsert inserts a new node before another node
    thisinsert = function(sBeforeId sTag sId)
    {
    var objSibling = this$(sBeforeId);
    objElement = documentcreateElement(sTag);
    objElementsetAttribute(idsId);
    objSiblingparentNodeinsertBefore(objElement objSibling);
    }

  thisgetInput = function(sType sName sId)
    {
    var Obj;
    if (sType == radio && !windowaddEventListener)
    {
    alert(here);
    Obj = documentcreateElement(<input type=radio id=+sId+ name=+sName+>);
    }
    else
    {
    Obj = documentcreateElement(input);
    ObjsetAttribute(typesType);
    ObjsetAttribute(namesName);
    ObjsetAttribute(idsId);
    }
    return Obj;
    }

  // xajaxcreateInput creates a new input node under a parent
    thiscreateInput = function(sParentId sType sName sId)
    {
    var objParent = this$(sParentId);
    var objElement = thisgetInput(sType sName sId);
    objParentappendChild(objElement);
    }

  // xajaxinsertInput creates a new input node before another node
    thisinsertInput = function(sBeforeId sType sName sId)
    {
    var objSibling = this$(sBeforeId);
    var objElement = thisgetInput(sType sName sId);
    objSiblingparentNodeinsertBefore(objElement objSibling);
    }

  // xajaxremove deletes an element
    thisremove = function(sId)
    {
    objElement = this$(sId);
    if (objElementparentNode && objElementparentNoderemoveChild)
    {
    objElementparentNoderemoveChild(objElement);
    }
    }

  //xajaxreplace searches for text in an attribute of an element and replaces it
    //with a different text
    thisreplace = function(sIdsAttributesSearchsReplace)
    {
    var bFunction = false;

  eval(var txt=documentgetElementById(+sId+)+sAttribute);
    if (typeof txt == function)
            {
                txt = txttoString();
                bFunction = true;
            }
    if (txtindexOf(sSearch)>)
    {
    var newTxt = ;
    while (txtindexOf(sSearch) > )
    {
    x = txtindexOf(sSearch)+sSearchlength+;
    newTxt += txtsubstr(x)replace(sSearchsReplace);
    txt = txtsubstr(xtxtlengthx);
    }
    newTxt += txt;
    if (bFunction)
    {
    eval(newTxt = + newTxt);
    eval(this$(+sId+)+sAttribute+=newTxt;);
    }
    else if (thiswillChange(sIdsAttributenewTxt))
    {
    eval(this$(+sId+)+sAttribute+=newTxt;);
    }
    }
    }

  // xajaxgetFormValues() builds a query string XML message from the elements of a form object

  thiscall = function(sFunction aArgs sRequestType)
    {
    var irpostData;
    if (documentbody && xajaxWaitCursor)
    documentbodystylecursor = wait;
    if (xajaxStatusMessages == true) windowstatus = Sending Request;
    if (xajaxDebug) thisDebugMessage(Starting xajax);
    if (sRequestType == null) {
       var xajaxRequestType = xajaxDefinedPost;
    }
    else {
    var xajaxRequestType = sRequestType;
    }
    var uri = xajaxRequestUri;
    var value;
    switch(xajaxRequestType)
    {
    ca** *ajaxDefinedGet:{
    var uriGet = uriindexOf(?)==??xajax=+encodeURIComponent(sFunction):&xajax=+encodeURIComponent(sFunction);
    for (i = ; i<aArgslength; i++)
    {
    value = aArgs[i];
    if (typeof(value)==object)
    value = thisobjectToXML(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 <aArgslength; i++)
    {
    value = aArgs[i];
    if (typeof(value)==object)
    value = thisobjectToXML(value);
    postData = postData+&xajaxargs[]=+encodeURIComponent(value);
    }
    } break;
    default:
    alert(Illegal request type: + xajaxRequestType); return false; break;
    }
    r = thisgetRequestObject();
    ropen(xajaxRequestType==xajaxDefinedGet?GET:POST uri true);
    if (xajaxRequestType == xajaxDefinedPost)
    {
    try
    {
    rsetRequestHeader(Method POST + uri + HTTP/);
    rsetRequestHeader(ContentType application/xwwwformurlencoded; charset= + xajaxCharEncoding);
    }
    catch(e)
    {
    alert(Your browser does not appear to  support asynchronous requests using POST);
    return false;
    }
    }
    ronreadystatechange = function()
    {
    if (rreadyState != )
    return;

  if (rstatus==)
    {
    if (xajaxDebug && rresponseTextlength < ) xajaxDebugMessage(Received:\n + rresponseText);
    else if (xajaxDebug) xajaxDebugMessage(Received:\n + rresponseTextsubstr()+\n[long response]\n</xajax>);
    if (rrespon***ML)
    xajaxprocessResponse(rrespon***ML);
    else {
    alert(Error: the XML response that was returned from the server is invalid);
    documentbodystylecursor = default;
    if (xajaxStatusMessages == true) windowstatus = Invalid XML response error;
    }
    }

  delete r;
    }
    if (xajaxDebug) thisDebugMessage(Calling +sFunction + uri=+uri+ (post:+ postData +));
    rsend(postData);
    if (xajaxStatusMessages == true) windowstatus = Waiting for data;
    delete r;
    return true;
    }

  // Tests if the new Data is the same as the extant data
    thiswillChange = function(element attribute newData)
    {
    if (!documentbody)
    {
    return true;
    }
    var oldData;
    if (attribute == innerHTML)
    {
    tmpXajax = this$(thisworkId);
    if (tmpXajax == null)
    {
    tmpXajax = documentcreateElement(div);
    tmpXajaxsetAttribute(idthisworkId);
    tmpXajaxstyledisplay = none;
    tmpXajaxstylevisibility = hidden;
    documentbodyappendChild(tmpXajax);
    }
    tmpXajaxinnerHTML = newData;
    newData = tmpXajaxinnerHTML;
    tmpXajaxinnerHTML = ;
    }
    eval(oldData=documentgetElementById(+element+)+attribute);
    if (newData != oldData)
    return true;

  return false;
    }

  //Process XML xajaxResponses returned from the request
    thisprocessResponse = function(xml)
    {
    if (xajaxStatusMessages == true) windowstatus = Processing;
    var tmpXajax = null;
    xml = xmldocumentElement;
    if (xml == null) {
    alert(Error: the XML response that was returned from the server cannot be processed);
    documentbodystylecursor = default;
    if (xajaxStatusMessages == true) windowstatus = XML response processing error;
    return;
    }
    for (i=; i<xmlchildNodeslength; i++)
    {
    if (xmlchildNodes[i]nodeName == cmd)
    {
    var cmd;
    var id;
    var property;
    var data;
    var search;
    var type;
    var before;

  for (j=; j<xmlchildNodes[i]attributeslength; j++)
    {
    if (xmlchildNodes[i]attributes[j]name == n)
    {
    cmd = xmlchildNodes[i]attributes[j]value;
    }
    if (xmlchildNodes[i]attributes[j]name == t)
    {
    id = xmlchildNodes[i]attributes[j]value;
    }
    if (xmlchildNodes[i]attributes[j]name == p)
    {
    property = xmlchildNodes[i]attributes[j]value;
    }
    if (xmlchildNodes[i]attributes[j]name == c)
    {
    type = xmlchildNodes[i]attributes[j]value;
    }
    }
    if (xmlchildNodes[i]childNodeslength > )
    {
    for (j=; j<xmlchildNodes[i]childNodeslength; j++)
    {
    if (xmlchildNodes[i]childNodes[j]nodeName == s)
    {
    if (xmlchildNodes[i]childNodes[j]firstChild)
    search = xmlchildNodes[i]childNodes[j]firstChildnodeValue;
    }
    if (xmlchildNodes[i]childNodes[j]nodeName == r)
    {
    if (xmlchildNodes[i]childNodes[j]firstChild)
    data = xmlchildNodes[i]childNodes[j]firstChilddata;
    }
    }
    }
    else if (xmlchildNodes[i]firstChild)
    data = xmlchildNodes[i]firstChildnodeValue;
    else
    data = ;

  var objElement = this$(id);
    try
    {
    if (cmd==al)
    {
    alert(data);
    }
    if (cmd==js)
    {
    eval(data);
    }
    if (cmd==in)
    {
    thisinclude(data);
    }
    if (cmd==as)
    {
    if (thiswillChange(idpropertydata))
    {
    eval(objElement+property+=data;);
    }
    }
    if (cmd==ap)
    {
    eval(objElement+property++=data;);
    }
    if (cm*==**)
    {
    eval(objElement+property+=data+objElement+property);
    }
    if (cmd==rp)
    {
    thisreplace(idpropertysearchdata)
    }
    if (cmd==rm)
    {
    thisremove(id);
    }
    if (cmd==ce)
    {
    thiscreate(iddataproperty);
    }
    if (cmd==ie)
    {
    thisinsert(iddataproperty);
    }
    if (cmd==ci)
    {
    thiscreateInput(idtypedataproperty);
    }
    if (cmd==ii)
    {
    thisinsertInput(idtypedataproperty);
    }
    if (cmd==ev)
    {
    eval(this$(+id+)+property+= function(){+data+;});
    }
    if (cmd==ah)
    {
    thisaddHandler(id property data);
    }
    if (cmd==rh)
    {
    thisremoveHandler(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;
    documentbodystylecursor = default;
    if (xajaxStatusMessages == true) windowstatus = Done;
    }
    }

  var xajax = new Xajax();


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25917.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.