Never been to DZone Snippets before?

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 (See related posts)

A handy, lightweight set of AJAX functions.

   1  
   2  function $(e){if(typeof e=='string')e=document.getElementById(e);return e};
   3  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};
   4  
   5  ajax={};
   6  ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
   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);return i.concat(s).concat(t).join('&');};
   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.get=function(url,func){ajax.send(url,func,'GET')};
  10  ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
  11  ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)};
  12  ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f)};
  13  ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};


How to use:

ajax.x - The XMLHttpRequest object (or MS equivalent) used for communication

ajax.serialize(f)
f = 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".

ajax.get(url, func)
url = the url to query (can contain arguments after a '?')
func = 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.

ajax.gets(url)
url = 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.

ajax.post(url, func, args)
url = the url to query
func = the function to call once the response is returned
args = 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.

ajax.update(url, elm)
url = the url to query
elm = 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.

ajax.submit(url, elm, frm)
url = the url to query
elm = the (name of the) element to update
frm = 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 on this post

Michael.Lowden posts on Feb 27, 2008 at 13:35
very nice code. i've adapted it a little to handle for IE's stupid Caching issue when using AJAX:

   1  
   2  function $(e){if(typeof e=='string')e=document.getElementById(e);return e};
   3  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};
   4  
   5  ajax={};
   6  ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
   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);return i.concat(s).concat(t).join('&');};
   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 ? '?' : '&') + 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 ? '?' : '&') + 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 ? '?' : '&') + 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))};


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)
Michael.Lowden posts on Feb 27, 2008 at 13:41
PS - the noCache versions just tweak the passed URL with:
   1  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:
   1  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:
   1  /_subpage1.jsp?param1=abc&param2=def&noCache=1204137560531

but without the words "noCache" it still works but is shorter:
   1  /_subpage1.jsp?param1=abc&param2=def&1204137560531
Michael.Lowden posts on Mar 11, 2008 at 01:50
well, started a new thread for this and updated it again. this time for "parse" ability.

check it out at:
http://snippets.dzone.com/posts/show/5182
timmorgan posts on Mar 11, 2008 at 07:54
I like your nocache stuff. I had actually done that myself in some of my apps due to IE caching stuff. I will probably update the get and put methods to tack that on since I believe that should be the default behavior.

You need to create an account or log in to post comments to this site.


Click here to browse all 5827 code snippets

Related Posts