~ overflow ~

Tag: xmlhttp

Simple AJAX/xmlhttp query issue

by z3n on May.20, 2009, under Coding, Tips & Hints

Problem:

You like jQuery, however, doing simple tasks get impossible since jQuery is too massive, even packed it’s over 50kb. The alternate, script.taculo.us, is even bigger!

Solution:

Doing a simple ajax or xmlhttp request, is nothing out of this world, actually, it’s pretty easy, you just need to watch it if you do more than one on the same page, or when a error happen you might want to have a handler.

Based on quirks mode example i wrote this little function, it’s able to do a rudimentar queue and return plain text from a remote server, it’s also compatible with IE.

var x=null;var p=0; // p is the processing control, while the request is running it will be true, if another request shows up it will fall into a timer loop until the first one ends
  1. _lD=function(url){
  2.    if(p==1){
  3.     setTimeout("_lD('"+url+"')",250);
  4.    }else{
  5.       p=1;
  6.       x=null;
  7.       if(window.XMLHttpRequest){
  8.          x=new XMLHttpRequest();
  9.       }else if(window.ActiveXObject){
  10.          x=new ActiveXObject("Microsoft.XMLHTTP");
  11.       }
  12.       if(x!=null){
  13.          x.onreadystatechange=function(){
  14.          var r="";
  15.          if(x.readyState==4){
  16.              if(x.status==200){
  17.                   r=x.responseText;
  18.  
  19.                   // manipulate your result here
  20.  
  21.                   p=0;
  22.                }
  23.             }
  24.          }
  25.          x.open("GET",url,true);
  26.          x.send(null);
  27.       }
  28.    }
  29. }

Packed, this function is less than 500 bytes, much better than the 58kb of jQuery, heheh

Leave a Comment :, , , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!