Twitter sharing button bit.ly integration on a static website

I am trying to integrate bit.ly on my website in JS to short my url. All my url are too long, what will be the most straight foward way to use the bit.ly restful api for sharing button on a static website in HTML/javascript.

The result I want to get is when my user click share on my website the url is automatically shortened by bit.ly

here is the code I am currently using to share my pages dynamically on twitter:

 <a href="javascript:tweetCurrentPage()" class="tweetlink">tweet this link</a>

<script type="text/javascript" charset="utf-8" src="http://bit.ly/javascript-api.js?version=latest&login=LOGINID&apiKey=APIKEY"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>

<script>

function tweetCurrentPage()
      { window.open("https://twitter.com/share?url=" + escape(window.location.href) + "&text=" + document.title, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600'); return false; }

var TweetThisLink = {

    shorten : function(e) {

        e.preventDefault();

        var url = this.href.substr(this.href.indexOf('http:',5))
        BitlyClient.shorten(url, 'TweetThisLink.response');
    },
    
    response : function(data) {
        var bitly_link = null;
        for (var r in data.results) {
            bitly_link = data.results[r]['shortUrl']; 
            break;
        }
        var tweet_text = "I am reading documentation of"
        document.location = "http://twitter.com/share?url=" + encodeURIComponent(tweet_text + ' ' + bitly_link);
    }
}


jQuery('.tweetlink').bind('click', TweetThisLink.shorten);
</script>

please tell me if my question is not clear enough. It’s my first development project I am just trying to learn.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.