How To Get The Last Tweet Using jQuery
Lots of websites have started using plugins that display the latest tweets from Twitter. Well, here’s a simple jQuery script shared by ayalcinkaya from Mkyong that uses jQuery which basically has the same concept except that it retrieves only the user’s one latest tweet.
jQuery AJAX
$('#last-tweet').click(function(){ $("#tweet_result").text("Loading......"); var username = $('#username').val(); $.ajax({ type: "GET", dataType: "json", url: "http://search.twitter.com/search.json?q=from:"+username+"&rpp=1&callback=?", success: function(data){ $("#tweet_result").text(data.results[0].text); } }); });
An HTML example
<html> <head> <title>How to Get Last Tweet with Jquery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> </head> <body> <h1>Get Last Tweet with jQuery</h1> username : <input type='text' id='username' size='50' value='mkyong' /> <br /> <br /> <h4> Last Tweet : <span id="tweet_result"></span> </h4> <button id="last-tweet">Get Last Tweet (.Ajax)</button> <script type="text/javascript"> $('#last-tweet').click( function() { $("#tweet_result").text("Loading......"); var username = $('#username').val(); $.ajax({ type : "GET", dataType : "json", url : "http://search.twitter.com/search.json?q=from:" + username + "&rpp=1&callback=?", success : function(data) { $("#tweet_result").text(data.results[0].text); } }); }); </script> </body> </html>
If you’d like to see the code in action, you can try it out here.