public

  • Download Gist
jquery-pluginization.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
(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);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.