-
-
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
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) { | |
// 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