jQuery URL shortener

Hi y’all,

My first jQuery plugin. Your welcome to use it (including commerical), re-code it and provide suggestions, please just keep reference to myself and to Uzbekjon.

It uses the http://bit.ly API so you’ll need your own account and API key. The code format is based on Mike Alsup’s Plugin Development Pattern.

Download/fork from Github: https://github.com/Kwozzie/jquery-url-shortener

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* jQuery Bit.Ly Shorten Url Plugin
* 2010 Justin Swan
* License: Creative Commons Attribution-Share Alike 3.0 Australia Licence
* Original Author: Uzbekjon (http://jquery-howto.blogspot.com/2009/04/shorten-long-urls-with-jquery-bitly.html)
* Version: 1.0.0
*
* Grateful for any feedback on improvements.
*
* Call: $(input field).shorten_url(long url)
* Returns: shortened url as value for input field.
*
*/
 
//
// start closure
//
(function($){
$.fn.shorten_url = function(url,options){
// Extend our default options with those provided.
var opts = $.extend($.fn.shorten_url.defaults, options);
 
return this.each(function(){
$this = $(this);
 
// format url to request
var daurl = "http://api.bit.ly/shorten?"
+"version="+opts.version
+"&longUrl="+url
+"&login="+opts.login
+"&apiKey="+opts.apiKey
+"&history="+opts.history
+"&format=json&callback=?";
 
// Utilize the bit.ly API
$.getJSON(daurl, function(data){
// Make a good use of short URL
$this.val(data.results[url].shortUrl);
});
});
};
 
$.fn.shorten_url.defaults = {
version: '2.0.1',
login: 'bitlyapidemo', // your bit.ly account login
apiKey: 'R_0da49e0a9118ff35f52f629d2d71bf07', // your api key here
history: '0',
};
//
// end of closure
//
})(jQuery);