Skip to content

Instantly share code, notes, and snippets.

@qwertypants
Last active September 24, 2015 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save qwertypants/671284 to your computer and use it in GitHub Desktop.
Save qwertypants/671284 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) {
var defaults = {
token: '',
longUrl: url
};
// Build the URL to query
var bitly = 'https://api-ssl.bitly.com/v3/shorten?' + '&access_token=' + defaults.token + '&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