Skip to content

Instantly share code, notes, and snippets.

@adatapost
Forked from cowboy/jquery-pluginization.js
Created July 13, 2010 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adatapost/473508 to your computer and use it in GitHub Desktop.
Save adatapost/473508 to your computer and use it in GitHub Desktop.
(function($){
// Store fetched long URLs here.
var cache = {};
$.fn.longUrl = function() {
return this.each(function(){
var elem = $(this),
href = elem.attr( 'href' ),
api = 'http://www.longurlplease.com/api/v1.1?callback=?';
if ( cache[ href ] ) {
// URL exists in cache, so use it.
elem.longUrlLengthen( cache[ href ] );
} else {
// Fetch JSON data.
$.getJSON( api, { q: href }, function(data){
if ( data[ href ] ) {
// If the data is valid, update the cache.
cache[ href ] = data[ href ];
// Call `longUrlLengthen` for this element + long url.
elem.longUrlLengthen( cache[ href ] );
}
});
}
});
};
$.fn.longUrlLengthen = function( url ) {
// Update the element's title attribute with `url`.
return this.attr( 'title', url );
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment