Posted By

praveensewak on 06/05/12


Tagged

javascript mini tiny pico


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

hurcy
domingl


mini-pico-tiny ajax


 / Published in: JavaScript
 

mini/micro javascript

  1. // mini-pico-tiny convenience micro-framework, ymmv --------------------------
  2. function $(id){ return document.getElementById(id); }
  3. function html(id, html){ $(id).innerHTML = html; }
  4. function css(id, style){ $(id).style.cssText += ';'+style; }
  5. function anim(id, transform, opacity, dur) {
  6. css(id, '-webkit-transition:-webkit-transform' +
  7. ',opacity ' + (dur||0.5) + 's,' + (dur||0.5) + 's;-webkit-transform:' +
  8. transform + ';opacity:' + (1||opacity));
  9. }
  10. function bindEvent(el, eventName, eventHandler) {
  11. if (el.addEventListener){
  12. el.addEventListener(eventName, eventHandler, false);
  13. } else if (el.attachEvent){
  14. el.attachEvent('on'+eventName, eventHandler);
  15. }
  16. }
  17. function stopEvent(e) {
  18. if (e.stopPropagation) e.stopPropagation();
  19. else e.cancelBubble = true;
  20.  
  21. if (e.preventDefault) e.preventDefault();
  22. else e.returnValue = false;
  23. }
  24. function log(text) { if (console && typeof console.log === 'function') { console.log(text); } }
  25. 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};
  26.  
  27. // mini-pico-tiny ajax
  28. var ajax={};
  29. ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
  30. 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('&');};
  31. 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)};
  32. ajax.get=function(url,func){ajax.send(url,func,'GET')};
  33. ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)};

Report this snippet  

You need to login to post a comment.