Custom tweet button with javascript

I was following the topic Build Your Own Tweet Button on https://dev.twitter.com/docs/tweet-button

They explicitly say

JavaScript Interfaces for Twitter are not available for custom Tweet Buttons.

But we can have a custom tweet button (or link) and still have the Twitter javascript  API (web intents).

All you have to do is to include the javascript file for the Twitter API:


<script src="https://platform.twitter.com/widgets.js"></script>

Your custom button (link in this example):


<a href="https://twitter.com/intent/tweet">Tweet</a>

This is the trick, instead of the link pointing to https://twitter.com/share

your link should point to https://twitter.com/intent/tweet

This way you still be able to use Twitter API. The following code shows how you can get

the tweet event:


twttr.events.bind('tweet', function (event) {
  // Do something there
  alert('Tweeted');
});

Check the online demo

Advertisements