DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Mini AJAX
<strong>Update: This code, along with docs and tests, are now available at <a href="http://github.com/seven1m/mini">http://github.com/seven1m/mini</a>.</strong>
A handy, lightweight set of AJAX functions.
function $(e){if(typeof e=='string')e=document.getElementById(e);return e};
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};
ajax={};
ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
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('&');};
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)};
ajax.get=function(url,func){ajax.send(url,func,'GET')};
ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)};
ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f)};
ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};
<strong>How to use:</strong> <strong>ajax.x</strong> - The XMLHttpRequest object (or MS equivalent) used for communication <strong>ajax.serialize(f)</strong> <em>f</em> = the form element you wish to be serialized 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". <strong>ajax.get(url, func)</strong> <em>url</em> = the url to query (can contain arguments after a '?') <em>func</em> = the function to call once the response is returned This function uses a GET request to query the specified url and return a response to the specified function. <strong>ajax.gets(url)</strong> <em>url</em> = the url to query (can contain arguments after a '?') 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. <strong>ajax.post(url, func, args)</strong> <em>url</em> = the url to query <em>func</em> = the function to call once the response is returned <em>args</em> = a string containing arguments to be passed to the url This function uses a POST request to query the specified url and return a response to the specified function. <strong>ajax.update(url, elm)</strong> <em>url</em> = the url to query <em>elm</em> = the (name of the) element to update This function uses a GET request to query the specified url and insert the result into the specified element. <strong>ajax.submit(url, elm, frm)</strong> <em>url</em> = the url to query <em>elm</em> = the (name of the) element to update <em>frm</em> = the form element to submit 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.






Comments
Snippets Manager replied on Fri, 2009/05/29 - 1:12am
Snippets Manager replied on Tue, 2009/02/24 - 4:44pm
Snippets Manager replied on Mon, 2012/05/07 - 2:23pm
Snippets Manager replied on Fri, 2009/02/06 - 8:24pm
Snippets Manager replied on Fri, 2009/02/06 - 8:24pm
Snippets Manager replied on Mon, 2012/05/07 - 2:23pm
Snippets Manager replied on Fri, 2009/02/06 - 8:24pm
Snippets Manager replied on Wed, 2008/02/27 - 2:28pm
Snippets Manager replied on Fri, 2008/12/05 - 8:45am
function $__(e){if(typeof e=='string')e=document.getElementById(e);return e}; function collect(a,f){var n=[];for(var i=0;i0){for(var i=0;i Heey! I edited it some, and it is my best ajax script!!! I added; - I changed $ function into $__ function because it makes problem if u use prototype.js - AJAX-JSON function called ajax.json(url); usage: come.php <? echo '{"name":"fatih"}'; ?> and ur function in another page:var obj = ajax.json('come.php'); alert(obj.name); //output alerts "fatih";- ajax.interval(url,div,interval), updates div periodically.. usage:ajax.interval('some.php','myDiv',1000); //update "myDiv" element every 1 seconds- ajax.timeout(url,div,timeout), updates div after a timeout..ajax.timeout('some.php','myDiv',1000); //update "myDiv" element after 1 seconds- edited ajax.update() and made ajax.update(url,elm,(true|false)) 3. parameter says if it is cached or not. - I solved cache problem with ajax.nocache function, you can look at! also i added a support that runs and calls javascript files in ajax requested page. for eg. if ur requested page includes alert('loaded!') or something, it runs and calls the file! i made it because some of javascript must be given in ajax pages... i hope it is useful!!!Snippets Manager replied on Mon, 2012/05/07 - 2:23pm
Snippets Manager replied on Wed, 2008/02/27 - 2:28pm
Snippets Manager replied on Wed, 2008/02/27 - 2:28pm
u = u + (u.indexOf('?') < 0 ? '?' : '&') + new Date().getTime();which just effectively adds a new query parameter of some fairly random digits. They don't need a "name" or a "value" to work, but if you want you can change the code to:u = u + (u.indexOf('?') < 0 ? '?' : '&') + 'noCache=' + new Date().getTime();this way it's a bit more obvious in the called URL what is happeneing with the gibberish, so you see something like:/_subpage1.jsp?param1=abc¶m2=def&noCache=1204137560531but without the words "noCache" it still works but is shorter:/_subpage1.jsp?param1=abc¶m2=def&1204137560531Snippets Manager replied on Wed, 2008/02/27 - 2:28pm
function $(e){if(typeof e=='string')e=document.getElementById(e);return e}; function collect(a,f){var n=[];for(var i=0;i I simply added the following functions: ajax.sendNoCache(url,func,method,args), not called by user, but called by some below functions. ajax.getNoCache(url, func) ajax.getsNoCache(url) ajax.postNoCache(url, func, args) ajax.updateNoCache(url, elm) ajax.submitNoCache(url, elm, frm)