How to get last Tweet via jQuery
Twitter, provides many APIs to deal with Tweets. In this example we are going to learn how to use jQuery to get last tweet of a user.
Twitter API
Get the last tweet of user (change “username” in the url with Twitter username you want)
http://search.twitter.com/search.json?q=from:username&rpp=1
For example, get last tweet from twitter’s user “mkyong”.
http://search.twitter.com/search.json?q=from:mkyong&rpp=1
1. jQuery Ajax
We use Jquery .ajax()
function to get the last tweet of the user and display it.
$('#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);
}
});
});
2. Full HTML Example
In this example, write the username into the box and click on the button to get last tweet of the user…
<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 />
<h2>
Last Tweet : <span id="tweet_result"></span>
</h2>
<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>
Try it yourself, type twitter username and click on the button.
Feel free to ask any question here…
I type hellojiger in the field and hit on get last tweet (.ajax) button it is not showing me any result. which API you are using ?
not work
Mkyong,
Apologize for submitting data manytimes, because of cache problem in the browser, could not see the latest comments which i posted, so i had to submit many times. So please delete the duplicate comments which i posted (last 4) and this comment.
Used for loop to get the latest two tweets it worked for me.
success : function(data) {
for(var i=0; i<2;i++){
$("#tweet_result").text(data.results[i].text);
$("#tweet_link").append("“+”Click Here“);
}//for loop
}//success
to know the attributes present in dataResults array use dataResults[index].toSource();
and customize your code based on the requirements.
Used for loop to get the latest two tweets it worked for me.
to know the attributes present in dataResults array use dataResults[index].toSource();
and customize your code based on the requirements.
Used for loop to get the latest two tweets it worked for me.
to know the attributes present in dataResults array use dataResults[index].toSource();
and customize your code based on the requirements.
Hey used for loop to get the latest two tweets it worked for me.
to know the attributes present in dataResults array use dataResults[index].toSource();
and customize your code based on your requirements.
ayalcinkaya,
Thankyou for this article. It helped me a lot..
Working on to get the last two latest tweets from Twitter. Do you have any thoughts on this. Is there a way to get the last two tweets.
Used for loop to get the latest two tweets it worked for me.
to know the attributes present in dataResults array use dataResults[index].toSource();
and customize your code based on the requirements.
The new Twitter API is doing away with this method, starting with blackout testing today. Any ideas on how to use the new API? The documentation is pretty opaque to me.
Remember, a number of the people come from a normal
income background, that is why they are hunting for things online.
For more information regarding our Internet marketing Services for
UK then kindly join us and send your proposal now.
Most often, bricks and mortar businesses spend far more than
$20 to market their products and services in local
newspapers, mail coupon campaigns, and other print advertising.
Thanks!!!
Nice piece of code.
But how can you get the last tweet to be displayed as link, a sort of “linkified” version of what you already have now?
Thanks!
Hi Serge,
You can create the link yourself using tweet id and profile name. I could not test it but it must work.
$(“#tweet_result”).text(“https://twitter.com/”+data.results[0].from_user_name+”/status/”+data.results[0].id);
change this and it gives you the url of the tweet.
I hope I understand the right thing…
Proper Code:
(Please note that this is to make the link show in addition to the already displayed text)
1. Changed id to id_str at the end
2. Changed from_user_name to from_user
3a. Changed #tweet_result to #tweet_link so you can display both the tweet content and the link
3b. Need to add
for the additional info to display.
-Yokai
ayalcinkaya,
Thanks for the help, meanwhile I went with the link-less version of it. Hopefully someone else will be able to use it soon enough.
Thanks again!
S.
I type fatabass in the field and click the get latest tweet but it does not work ?
fatabass’s account is protected. Twitter just giving public twitter acoount’s tweet…
Thank’s for reporting bug