Skip to content

Instantly share code, notes, and snippets.

@Lasha
Forked from qwertypants/jQuery_bitlyfi.js
Created July 28, 2012 05:07
Show Gist options
  • Save Lasha/3191889 to your computer and use it in GitHub Desktop.
Save Lasha/3191889 to your computer and use it in GitHub Desktop.
Create a bit.ly URL by passing the URL you want shortened and preforming a function with it
function bitlyfi(url, func) {
// Put your own login and apiKey
var defaults = {
login: '',
apiKey: '',
longUrl: url
};
// Build the URL to query
var bitly = "http://api.bit.ly/v3/shorten?" + "&login=" + defaults.login + "&apiKey=" + defaults.apiKey + "&longUrl=" + defaults.longUrl + "&format=json&callback=?";
// Utilize the bit.ly API
$.getJSON(bitly, function (results) {
if (results.status_txt === "OK") {
return func(results.data["url"]);
}
else {
return func(url);
}
});
}
//Example usage
$('a').click(function (e) {
e.preventDefault();
var href = $(this).attr('href');
bitlyfi(href, function (bitlyfiedURL) {
alert(bitlyfiedURL);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment