Quantcast
Viewing all articles
Browse latest Browse all 16

Twitter, tweet button with javascript callback

Here is an example of using Twitter Web intents with js events callback.  This is the html anchor tag.

<a href="https://twitter.com/intent/tweet?text=&amp;url=http://youtubeplaylist.net&amp;via=ytubeplaylist" class="twitter-share-button">Tweet</a>

To allow the js events, you must include the widget.js to make it working. Here is the code in below,

    window.twttr = (function (d, s, id) {
        var t, js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return; js = d.createElement(s); js.id = id;
        js.src = "//platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
        return window.twttr || (t = { _e: [], ready: function (f) { t._e.push(f) } });
    }(document, "script", "twitter-wjs"));


Next, we use twttr.ready() to to bind events in this method, we can bind several events like, tweet, follow, retweet, favorite, click,

twttr.ready(function (twttr) {
        twttr.events.bind('tweet', function (event) {
            // Do something there
            alert("callback");
        });
		twttr.events.bind('follow', function(event) {
    		var followed_user_id = event.data.user_id;
    		var followed_screen_name = event.data.screen_name;
		});

		twttr.events.bind('retweet', function(event) {
		    var retweeted_tweet_id = event.data.source_tweet_id;
		});

		twttr.events.bind('favorite', function(event) {
		    var favorited_tweet_id = event.data.tweet_id;
		});
});

For more information, check out the twitter documentation, https://dev.twitter.com/docs/intents/events#waiting-for-asynchronous-resources


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 16

Trending Articles