Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!news.mixmin.net!news2.arglkargh.de!noris.net!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <1399593.26SQpm16Mm@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Fri, 07 Sep 2012 23:44:37 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 79 NNTP-Posting-Date: 07 Sep 2012 23:44:37 CEST NNTP-Posting-Host: 81812045.newsspool1.arcor-online.net X-Trace: DXC=cJ\BWmKP_AC02Sh8E_NfIAic==]BZ:afN4Fo<]lROoRAnkgeX?EC@@@IRTKiQcV`1DDZm8W4\YJNLR=i2=[N6Y6J4^f?YmnoooD]k1ao;KaV9H X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:15784 Tim Streater wrote: > Gene Wirchenko wrote: >> > By the way, as has been pointed out you need to check that status==200 >> > also. You also need to set xhr = null when you've finished with it. >> >> I know about 200. Do you know about 0 and 1223 (sic!) as well? >> Setting xhr to null? Why? I may well be using it again. The >> forms are CRUD and so there may be multiple transactions go to the >> server. ACK. > And each time you call your ajax function you'll get a new one. Which is not a bad thing. > AIUI, although my knowledge of closures is a bit limited, each one will > live forever unless you explicitly dump it when you've finished with it. > By setting it to null, it'll get garbage-collected subsequently. Wishful thinking. There is no guarantee that garbage collection will commence before all browser windows are closed. > Here's what I do (slightly simplified, as I log the status error rather > than just alert-ing): > > function ajax (url, data, callbackFunction) > { > > var request = window.XMLHttpRequest ? new XMLHttpRequest () : new > ActiveXObject ("MSXML2.XMLHTTP.3.0"); > > request.open ("post", url, true); > request.setRequestHeader ("Content-Type", > "application/x-www-form-urlencoded; charset=utf-8"); > > request.onreadystatechange = function () > { > > if (request.readyState!=4) return; > > if (request.status==200 && request.responseText!="") > callbackFunction (request.responseText); > if (request.status!=200) alert ("Bad ajax status: " + > request.status); > > request = null; With regard to memory efficiency and probably even runtime efficiency, it is far better to reuse an existing XHR object once the request has been processed. See JSX:http.js [1] for example. > }; > > request.send (data); `data' needs to be transfer-encoded; with this Content-Type header field value and without a Transfer-Encoding header field, it needs to be percent- encoded: request.send(encodeURI(data)); But you might want to encodeURIComponent() each name an value separately instead. PointedEars ___________ [1] -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs,