miniAJAX.js - updated to be a bit more robust
see CHANGELOG in details.
1 function $(e){if(typeof e=='string')e=document.getElementById(e);return e}; 2 function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n}; 3 4 ajax={}; 5 ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}}; 6 //ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');}; 7 ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);var sOutput = i.concat(s).concat(t).join('&'); while (sOutput.indexOf('&&')>0) sOutput = sOutput.replace("&&","&"); return sOutput;}; // i've modified to clear out instances of "&&" in the POSTback 8 ajax.send=function(u,f,m,a){var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)}; 9 ajax.sendNoCache=function(u,f,m,a){u = u + (u.indexOf('?') < 0 ? '?' : '&') + 'noCache=' + new Date().getTime();var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)}; 10 ajax.get=function(url,func){ajax.send(url,func,'GET')}; 11 ajax.getNoCache=function(url,func){url = url + (url.indexOf('?') < 0 ? '?' : '&') + 'noCache=' + new Date().getTime();ajax.send(url,func,'GET')}; 12 ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText}; 13 ajax.getsNoCache=function(url){url = url + (url.indexOf('?') < 0 ? '?' : '&') + 'noCache=' + new Date().getTime();var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText}; 14 ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)}; 15 ajax.postNoCache=function(url,func,args){ajax.sendNoCache(url,func,'POST',args)}; 16 ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f);}; 17 ajax.updateNoCache=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.getNoCache(url,f);}; 18 ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))}; 19 ajax.submitNoCache=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.postNoCache(url,f,ajax.serialize(frm))}; 20 21 22 23 /** HOW TO USE 24 ajax.x - The XMLHttpRequest object (or MS equivalent) used for communication 25 26 ajax.serialize(f) 27 f = the form element you wish to be serialized 28 This function serializes all the fields in a form so that they can be passed as a query string in the form 'arg1=val1&arg2=val2'. 29 30 ajax.getNoCache(url, func) 31 ajax.get(url, func) 32 url = the url to query (can contain arguments after a '?') 33 func = the function to call once the response is returned (no quotes) 34 This function uses a GET request to query the specified url and return a response to the specified function. 35 36 ajax.getsNoCache(url) 37 ajax.gets(url) 38 url = the url to query (can contain arguments after a '?') 39 This function uses a GET request to query the specified url and return a response synchronously. Use this sparingly, as synchronous calls can lock up the browser. 40 41 ajax.postNoCache(url, func, args) 42 ajax.post(url, func, args) 43 url = the url to query 44 func = the function to call once the response is returned (no quotes) 45 args = a string containing arguments to be passed to the url 46 This function uses a POST request to query the specified url and return a response to the specified function. 47 48 ajax.updateNoCache(url, elm) 49 ajax.update(url, elm) 50 url = the url to query 51 elm = the (name of the) element to update 52 This function uses a GET request to query the specified url and insert the result into the specified element. 53 54 ajax.submitNoCache(url, elm, frm) 55 ajax.submit(url, elm, frm) 56 url = the url to query 57 elm = the (name of the) element to update 58 frm = the form element to submit 59 This function is typically used in the onsubmit handler of a function. The form is not submitted the usual way; the form is instead serialized using 'ajax.serialize' and submitted using 'ajax.post'. The result is then inserted into the specified element. 60 */ 61 62 63 /** CHANGES - by Michael Lowden 64 [2/27/2008 10:43 AM] 65 Update the "serialize" function to get rid of risk of consecutive "&" symbols. where "&&&&&&" may throw either an exception or a warning on some webservers (e.g. Tomcat). 66 67 [2/26/2008 4:47 PM] 68 Adapted it a little to handle for IE's stupid Caching issue when using AJAX: 69 I simply added the following functions: 70 ajax.sendNoCache(url,func,method,args), not called by user, but called by some below functions. 71 ajax.getNoCache(url, func) 72 ajax.getsNoCache(url) 73 ajax.postNoCache(url, func, args) 74 ajax.updateNoCache(url, elm) 75 ajax.submitNoCache(url, elm, frm) 76 */