19 Feb 10 — 0

jQuery AJAX Code Abuse

This code snippet doesn't quite qualify as a Weecode at 150 characters, but it's a great demonstration of what can be done with a bit of jQuery and a blatant disregard for programming technique. This is a jQuery extension that enables AJAX content loading, with caching, on a valid link selector:

$.fn.cc=function(t){$(this).click(function(){
return!((d=(o=$(this)).data(h=this.href))?$(t).html(d):$.get(h,function(d){o.data(h,d);$(t).html(d)}))})
}

If you prefer a more - ahem - readable version:

$.fn.cc = function(t) {
   $(this).click(function(){
      return!(
         (d=(o=$(this)).data(h=this.href))
         ?$(t).html(d)
         :$.get(h,function(d){o.data(h,d);$(t).html(d)})
      )
   })
}

For example, the following code will tell links with the class "load" to insert the content at their URL into the div with the id "ajax-content":

$('a.load').cc('#ajax-content');

The first time the URL is requested the content will be loaded into the div. Subsequent calls will be loaded instantly, forgoing the unneeded HTTP request. This is particularly useful for pagination.

"But browsers can cache requests internally" you say. Well, yes, and no. Caching behaviour is unpredictable, and anyway, you're missing the point.

For the sake of completeness, a jQuery extension should handle library conflicts, the possibility that it will be auto-magically appended to other scripts, allow for chaining, and support failed AJAX requests:

;(function($){
   $.fn.cc = function(t) {
      return $(this).click(function(){
         return!(
            (d=(o=$(this)).data(h=this.href))
            ?$(t).html(d)
            :$.get(h,function(d,s){
               if(s=="success")
                  o.data(h,d) && $(t).html(d)
               else
                  window.location=h
            })
         )
      })
   }
}(jQuery));

As an added bonus, you can cache requests before the user even clicks on the link. I like to pre-cache the 'next' link within pagination areas.

$.fn.precache = function() {
   return $(this).each(function() {
      o=$(this);h=this.href;
      $.get(h,function(d,s){
         if(s=="success") o.data(h,d);
      });
   });
}

Pre-cache content by:

$('.next').precache();

Download the code here.

Or, for a much more readable version: use this file.

Add your comment »

3 Feb 10 — 0

Weecode Snippets - Part 1

JavaScript really is the hobbyists language. Everyone has two or three run-time environments sitting around, it has some interesting language features and it is genuinely useful. Of course, that doesn't stop people like me coming up with generally useless things to do with it.

And so I present: Weecode. Weecodes are JavaScript snippets in 140 characters or less - conveniently short enough for a tweet! If you feel like contributing, send your wee functions to @weecode and I'll post them back. At some point there will be a page here where you can vote and tag snippets.

I'm looking for particularly tiny, clever or obscure code. The fewer 'higher level' constructs you use the better (who needs a pesky Math object, anyway?). Does it appear to solve quadratic equations while calculating the volume of a sphere? Even better.

The idea is to show off your abilities, have fun and maybe learn something new along the way.

To start things moving, I've posted two "useful" functions:

Figure out the nth Fibonacci sequence number - 66 characters:

function F(n){p=0;n*=2;while(n--){p-=4/((n--)*(n%4-1));}return p;}

Calculate sin of x in n iterations (n > 6 is basically as good as Math.sin) - 105 characters:

function sin(x,n){n*=2;r=0;while(n--){for(i=(f=n),p=x;--i;f*=i,p*=x){}r-=(n--%4-2)*p/f}return r;}

Add your comment »