Back jQuery

Get The Latest Twitter Tweet with jQuery

WRITTEN BY ON 23 Jan 2011
41,141 VIEWS • SHARES
13 comments

Quick and easy way to retrieve the lastest twitter tweet from any user. It uses twitter user timeline api to display the latest tweet. Also, it checks for URL and reply tags and convert them into URL links.

JS - jQuery

You just have to create a div or p with id twitter like below:

<p id="tweet"></p>

and it should work perfectly. :)

For those who wants more advance feature, visit two of my tutorials in queness


$(document).ready(function() {
 
    // set your twitter id
    var user = 'quenesswebblog';
     
    // using jquery built in get json method with twitter api, return only one result
    $.getJSON('http://twitter.com/statuses/user_timeline.json?screen_name=' + user + '&count=1&callback=?', function(data) 		{
         
        // result returned
        var tweet = data[0].text;
     
        // process links and reply
        tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, function(url) {
            return ''+url+'';
        }).replace(/B@([_a-z0-9]+)/ig, function(reply) {
            return  reply.charAt(0)+''+reply.substring(1)+'';
        });
     
        // output the result
        $("#tweet").html(tweet);
    }); 
     
});
Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

13 comments
Mario 12 years ago
This is fantastic. Is there a way to also include the date of the tweet? Thanks!
Reply
ramses 12 years ago
works great with normal twits but it'snt display anything when the last tweet is a rewet
Reply
Florian 12 years ago
Very good! Like it :)
Reply
Krishna Khanna 12 years ago
Works great kkonline.org -Inspiring Life...
Reply
Dave 11 years ago
@Ramses - Add &include_rts=1 after &count=1 in your get request.

@Kevin - good job on the link formatting.
Reply