Last active
September 24, 2015 04:57
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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