PDA

View Full Version : Shorten URL using bit.ly (Javascript)



Jahova
09-01-2010, 01:27 PM
Hello there. For my forum I wish to use Javascript to shortern the page url into a bit.ly URL and then for the bit.ly URL to be put into a Javascript var ready to be called in my twitter re-tweet script.

You can find my current twitter re-tweet script below. At the moment it can get the page URL and title and then tweet these. What I want it the page URL to be first shortened using bit.ly (js) and then the new short-url echoed instead of twtUrl.


// Variables for twitter to use
var twtTitle = document.title;
var twtUrl = location.href;
var maxLength = 140 - (twtUrl.length + 1);
if (twtTitle.length > maxLength) {
twtTitle = twtTitle.substr(0, (maxLength - 3))+'...';
}
var twtLink = 'http://twitter.com/home?status='+encodeURIComponent(twtTitle + ' ' + twtUrl);
document.write('<a href="'+twtLink+'" target="_blank"'+'>Tweet This!<'+'/a>');

I hope you understand what I mean. Kind regards.

Luke
09-01-2010, 01:29 PM
Probs not right, but could any of this be any use?

http://code.google.com/p/bitly-api/wiki/ApiDocumentation

Jahova
09-01-2010, 01:31 PM
@LukeBateson: I've attempted to use the API demos, but as I have very limited JS knowledge, I am not able to turn the demo; http://bit.ly/app/demos/shorten.html

Into somthing that will output a var for me to use in my twitter script.

Luke
09-01-2010, 01:37 PM
@LukeBateson: I've attempted to use the API demos, but as I have very limited JS knowledge, I am not able to turn the demo; http://bit.ly/app/demos/shorten.html

Into somthing that will output a var for me to use in my twitter script.

Well, I'm not really a Javascript guru myself, so can't help lol.
Sorry

Luke

Rep returned

Jahova
09-01-2010, 01:52 PM
Thanks and no problem. Anyone else? Should be a simple job, I would think.

Jahova
09-01-2010, 05:33 PM
Bump, will offer +REP for help!

Invent
09-01-2010, 06:26 PM
Try something like this (you'll need to change the Bit.ly API key, etc):


<script type="text/javascript" charset="utf-8" src="http://bit.ly/javascript-api.js?version=latest&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07"></script>
<script type="text/javascript">
<!--

document.write( '<div id="tweet-this"></div>' );

BitlyClient.addPageLoadEvent(function(){

BitlyCB.myShortenCallback = function( data ) {

var result;

for( var r in data.results ) {

result = data.results[ r ];
result[ 'longUrl' ] = r;

break;

}

var twtTitle = document.title;
var twtUrl = result[ 'shortUrl' ];
var maxLength = 140 - ( twtUrl.length + 1 );

if( twtTitle.length > maxLength ) {

twtTitle = twtTitle.substr( 0, ( maxLength - 3 ) ) + '...';

}

var twtLink = 'http://twitter.com/home?status=' + encodeURIComponent( twtTitle + ' ' + twtUrl );

document.getElementById( 'tweet-this' ).innerHTML = '<a href="' + twtLink + '" target="_blank">Tweet This!</a>';

}

BitlyClient.shorten( document.location, 'BitlyCB.myShortenCallback' );

});

-->
</script>

Jahova
09-01-2010, 06:41 PM
Worked brilliantly. Thank you. Now time to customize it :)

Sorin
11-01-2011, 12:10 PM
Great script! Works perfect for me too! Is it possible for this tweeter custom button to also include a twitter username (just like the official button does ex: via @johndoe) that we specify? 10x in advance! :)

Want to hide these adverts? Register an account for free!