Skip to content

Instantly share code, notes, and snippets.

@jc1arke
Created May 10, 2010 10:42
Show Gist options
  • Select an option

  • Save jc1arke/395914 to your computer and use it in GitHub Desktop.

Select an option

Save jc1arke/395914 to your computer and use it in GitHub Desktop.
var enableRefresh = true; // Set this to true if you want it to continously check for new tweets, otherwise set it as false
// Execute only once the *full* document has rendered to the user's browser
jQuery(document).ready( function() {
loadTweets( "twitter_name_here", 1, 'ul.latest_tweet' ); // Call function loadTweets (pay attention to the "1" ;) ;)
});
var loadTweets = function(user, count, element) {
// Set the URL
twitter_url = "http://twitter.com/status/user_timeline/" + user + ".json?count=" + count + "&callback=?";
// Retrieve the feed :)
jQuery.getJSON(twitter_url, function(data) {
jQuery.each(data, function(i, item) {
var txt = item.text
// Change the text links to active links with a target of new window
.replace(/(https?:\/\/[-a-z0-9._~:\/?#@!$&\'()*+,;=%]+)/ig, '<a href="$1" target="_blank" title="$1">$1</a>')
// Change references to other Tweeters to links
.replace(/@+([_A-Za-z0-9-]+)/ig, '<a href="http://twitter.com/$1" target="_blank" title="$1 on Twitter">@$1</a>')
// Change hashtags to active links
.replace(/#+([_A-Za-z0-9-]+)/ig, '<a href="http://search.twitter.com/search?q=$1" target="_blank" title="Search for $1 on Twitter">#$1</a>');
// Add the tweet to the list of tweets :)
if( count == 1 ) {
// Clear previous tweets
jQuery(element).html("");
jQuery( "<li />", {html : txt}).appendTo(element);
} else {
jQuery('<li>' + txt + '</li>').appendTo(element);
}
});
});
// If refresh is enabled, it will call this function every 5 seconds
if( enableRefresh == true ) {
setTimeout( "loadTweets('" + user + "', " + count + ", '" + element + "')", 5000 );
}
}
<!doctype html>
<html>
<head>
<title>My Tweets Update Page</title>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script>
google.load('jquery', '1.4.2'); // Latest version of jQuery
</script>
<script src="tweets.js" type="text/javascript"></script>
</head>
<body>
<ul class="latest_tweet">
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment