with Imagination: by Dustin Diaz

./with Imagination

A JavaScript, CSS, XHTML web log focusing on usability and accessibility by Dustin Diaz

Unofficial Twitter Widget Documentation

Friday, July 24th, 2009

So, the Twitter Search Widget has officially launched. And the installation is fairly self-explanatory (as a matter of fact, I don’t think we even explained it at all??). But nonetheless, if you haven’t checked it out, it’s worth doing that now. The new widgets are hot! Ok. On with this.

So, what’s this do?

The Twitter Search Widget allows you to track any topic, live, in real-time. You can even use our advanced search operators which dogfoods straight into our own search API.

So what are these other features?

Well, for the advanced JavaScripters; or actually, I should say, ANYONE who can read JavaScript should be able to try some of these things out on your own. Which brings us to the hidden undocumented features

But first…

Let’s look at the base widget code to start off with.

base twitter widget code

<div id="twtr-search-widget"></div>
<script src="http://widgets.twimg.com/j/1/widget.js"></script>
<link href="http://widgets.twimg.com/j/1/widget.css" type="text/css" rel="stylesheet">
<script>
new TWTR.Widget({
  search: 'my search query',
  id: 'twtr-search-widget',
  loop: true,
  title: 'what people say about...',
  subject: 'stuff and things',
  width: 250,
  height: 300,
  theme: {
    shell: {
      background: '#111',
      color: '#000000'
    },
    tweets: {
      background: '#000000',
      color: '#ffffff',
      links: '#f59f00'
    }
  }
}).render().start();
</script>

What this does is load our base functionality and stylesheet. It then instantiates a new Widget object, and points to an id representing an HTML element. What this tells you already is that you could technically have multiple widgets on a page by simply instantiating new objects, populating existing <div> elements. Hey, you could create your own web version of tweet deck this way by stacking a few of them into columns :)

The features object

So there is a features object that you can pass into your constructor. There are currently four of these features.

the features object

new TWTR.Widget({
  id: 'my-element-id',
  .
  .
  .
  features: {
    hashtags: true,
    timestamp: true,
    avatars: true,
    fullscreen: false
  }
}).render().start();

Albeit mostly self explanatory, they each do this:

  • hashtags: defaults to true. setting to false will hide them.
  • timestamp: defaults to true. setting to false will hide them.
  • avatars: defaults to true for search. and false for profile widget. setting to false will hide them.
  • fullscreen: (I love this feature!) defaults to false. setting to true will give you a full viewport sized widget, perfect for displaying on large screens at events, conferences, etc.

Overall, these features are pretty great. I especially love the hashtag and fullscreen features since they are perfect for pairing.

Let’s say for instance you’re running a conference like SxSW — you could search for “#sxsw OR #sxsw09″ — and in the results, hide the hashtags so it doesn’t distract the readability of all the searches. Neat, eh?

Some examples

I went ahead and installed the widget on my own site — And here’s a few examples:

That’s it for now

This will all undoubtedly be on our FAQ website soon enough, but it’s enough to have a play.

Happy Twidget Searching!

52 Responses to “Unofficial Twitter Widget Documentation”

  1. Ryan McGrath

    Haha, wow. I didn’t realize this was something that was being worked on at Twitter - just recently launched http://twitstat.us with a friend, but it looks like it was for naught, eh?

    Ah well, competition’s awesome. Nice job on the widget!

  2. James

    Nice work!

    One question; why not just dynamically load the StyleSheet within the JavaScript? And there’s no need for the DIV (”twtr-search-widget”), or the additional SCRIPT tag. In fact, you could just get away with this: http://gist.github.com/153958 (everything else can added dynamically, and the innerText of the SCRIPT tag can be globally eval’ed - see jQuery.globalEval)

    Just an idea… To make it simpler :)

  3. Ideas at random ...

    I clicked on the examples, but all that displayed was a black background and a Twitter logo.

    Tried the example on Firefox and Chrome.

  4. Jim

    Nice work, bro. And thanks for the insider tips!

  5. Kevin

    It would be great if the HREF’s utilized TARGET=”_blank” so that links would render in a new window (or tab). I would love to add this widget to my site, but am hesitant because it will divert users off of my page.

  6. Abhi

    If retweets are defaulted to off, how do we turn them back on?

    Also, is there a way to customize the profile or search widget to only show one’s favorited tweets?

    Any help would be greatly appreciated.

  7. Eric Herboso

    Thanks so much for creating this widget! I work for a nonprofit and we’re using it tonight for a hashtag virtual tweetup that benefits feeding hungry children — and we couldn’t do it nearly so gracefully without your help!

  8. Ben Evans

    ok, so how do I make it show the latest tweet on the top instead of pushing it down? as it loads, it ends up pushing the latest tweet off the visible area leaving older comments visible.

  9. Tylor

    search the google then find your site.
    that is a really excellent post.

    Thanks for the post.

  10. Dustin Diaz

    @Jim, thanks :)

    @Kevin, noted. will most likely become a feature.

    @Abhi It’s in your best interest that they’re off according to our research. Really.
    On another note, regarding your other question — Yes! There is a way (also undocumented).

    In your constructor…

    new TWTR.Widget({
        ...
        favs: true,
        ...
    }).render().setUser('your_screen_name').start();

    That should get you what you want. There is an example on my homepage.

    @Eric that’s great to hear.

    @Ben most likely a future version will have no effects with just a simple scroll bar

    @Tylor :)

  11. Chris Ward

    I’d like an option to have the latest tweet at the top and for it to stay there until superceeded by a newer one, not dissappear off the bottom.
    Also a parameter to state the maximum number of historical tweets to list.
    Allowing brackets in advanced search would be nice too, but I guess that’s not your area :-)

  12. ecw

    I have to agree with Ben - seems like most recent tweets should be on top - even in a future version without effects and a scroll bar.

  13. Warren

    Nice widget!

    Is there a way to set the effect delay?

    Is there a non-minified version of the js available?

    Thanks!

  14. dbegor

    how about a target for links? target: ‘Blank’, ??

  15. Dustin Diaz

    hey guys, sorry for the delay in replying.

    @Chris Ward, currently, the best way to do this is to turn off looping. No old tweets will appear. And when it finds a new one… it will show up at the top.

    and regarding [brackets]… do you mean “quotes”? because that “does” work.

    @ecw, this may just be a matter of confusion. the most recent tweets always come in first since it is the most important — and they are loaded from the bottom up… so it’s reversed. does that make sense? trust me, it would be awkward the other way around if you had the oldest tweets always load in first. you would perhaps never see the latest tweets thereby never getting the true value of it being “live”

    @Warren, you can set the ‘interval’ property in the constructor object to a longer time in milliseconds. currently the default is 7000 (the time between each tweet). do this as such:

    new TWTR.Widget({
        ...
        interval: 12000, // time in ms
        ...
    })

    @dbegor noted.

  16. Josh Webb

    First—let me say that this is awesome. Really nice work.
    Second—is there any way to limit how far into the past it reaches? I’d like to keep it from showing tweets from, say, a year ago.

  17. Dustin Diaz

    Hi Josh,
    Let me first just say, thanks — and you’ll most likely ‘never’ have to worry about that problem of showing tweets that long ago. try a few sample searches out on the demo page, and note how far back they go.

  18. Chris

    Thanks for the new Widgets Dustin :)

    Though I seem to be experiencing the problem Josh mentioned above… Tweets from a year ago are appearing (with and without the loop). If I place the widget further down the page on my blog, by the time a guest sees it, it shows some really old tweets.

    I know you’ve tried to create a scrolling “live” feed look… but I think I’d prefer a “static” widget that only displayed the latest tweets, with the newest one at the top. If a new tweet came in, it would pop-in at the top and push the older ones down. That way visitors would be guaranteed of only seeing the most recent ones.

    It would also be nice if links could open in a new page… I’m not that worried about loosing traffic on my personal blog, but I wouldn’t want to take traffic away from some of my more popular sites.

    Thanks again, and keep up the good work! Cheers

  19. Patrick

    I’m using the profile widget on my site and I want to change some things but I don’t know how.

    1st I want to change it so that if you click on a link it opens in a new tab or window so that it doesn’t link away from my site.

    2nd I only want to to display the updates made within the past 21 days.

    Can you help me out?

    new TWTR.Widget({
    profile: true,
    id: 'twtr-profile-widget',
    loop: true,
    width: 230,
    height: 250,
    theme: {
    shell: {
    background: '#dcdcdc',
    color: '#434343'
    },
    tweets: {
    background: '#ffffff',
    color: '#444444',
    links: '#0389ff'
    }
    }
    }).render().setProfile('BCSProSoft').start();

  20. Ryan

    Great widget. Anyway to change the order. It seems to be in descending order which doesn’t make sense. I want to see the newest tweets always at the top and the older ones move towards the bottom eventually out of view.

  21. Tony Restell

    Really fantastic application, well done!

    Have encountered one issue though - tweets including hashtags not appearing in my search widget regardless of what the hashtag feature is set to.

    http://www.top-consultant.com/twitter_consulting_news.asp

    This has the search parameters set as

    to:tonyrestell OR from:tonyrestell OR tonyrestell

    However I have received a tweet from @sayanghosh that is not appearing on the above search page. Conduct the same search on twitter search and it appears. There’s no RT in the message so that’s not the source of the problem.

    Any ideas how to resolve? Thanks! Tony

  22. Marieke

    Anybody have any idea to make the widget SSL?

  23. John Flanagan

    Hi Dustin,
    I just added your profile widget to our website (mrinkbee.com) and noticed that the link to Join the conversation takes me away from our website.
    I tried using _blank for the link target attribute but that doesn’t work.
    Any suggestins?

  24. Chris

    Hi Dustin -

    Quick question for you. I changed my Twitter picture, but the Profile Widget still calls for the old picture that no longer exists. Twitter and the old flash widget call the new picture, but not the new widget. What’s up with that? It’s driving me a bit crazy. Thanks!

  25. Lou Hornberger

    Love the widget. One problem I’m having is that the widget won’t load my Twitter icon/avatar it uses the default o_O one in all three versions.

    http://www.twitter.com/SUMOYouth shows the icon

    but sumo.SalemUM.com doesn’t.

    To make sure it wasn’t a TYPO3 issue i created this page: http://www.salemum.com/revo.html to test it and when I try to login I get a “This method requires a GET” error message.

    Thanks!

  26. Mark Robbins

    I am new to Twitter but am confused about the pound sign preceding words. I do not see reference to it in the help section. I think this is what you are referring to as a hashmark but what it the function of the pound sign preceding a word? I would appreciate an email reply if possible. - MR

  27. zoblue

    Is there a way to affect the time stamp? Instead of ‘x days ago’ can the script put the actual time stamp?

  28. Endora

    Dustin,
    I would like to add this widget to a govt site but govt law dictates that all code must be 508 compliant. The only problem we are having with the widget is that the images are missing alt attributes. Any way to easily do this? I can handle the header and footer images using jquery but cannot handle the tweet imgs since they are intermittent. Any help or ideas would be appreciated. thanks.

  29. Dustin Diaz

    Hi Patrick,
    try the search widget instead… and pass this to your search:

    
    search: "from:BCSProSoft since:" + twentyOneDaysAgo(new Date())

    The rest is up to you to kinda do the math.

    Endora,
    I sent you an email - and i’ve filed a ticket to put in the alt attribute

    Chris,
    it needs to proprogate in the API. i know, it can be frustrating. it should be working by now though.

    Tony,
    I fixed that bug :) (it was a bug)

    Josh,
    check out the advanced operators
    http://search.twitter.com/operators

  30. Damian Smith

    Hi Dustin,

    I’m also trying to get the search widget to only display posts from the last 24hours. Is this possible? If so, would you mind sharing the code with us?

    Thanks

  31. Damian Smith

    After a couple of hours of playing around with some simple Javascript, I’ve worked out how to limit how far it will search prior from the current day.

    dSearch = new Date();
    var numDays = X; //days prior to current day
    dSearch.setDate(dSearch.getDate() - numDays)

    sMonth = '00' + (dSearch.getMonth() + 1);
    sMonth = sMonth.substr(sMonth.length - 2);

    searchDate = dSearch.getFullYear() + "-" + sMonth + "-" + dSearch.getDate();

    new TWTR.Widget({
    search: "(your search here) since:" + searchDate,

  32. Dave

    Good work. But please don’t dictate how you think we want to sort the tweets. I also think the newest tweets should be on the top of the list. If a newer tweet comes in while a user is on the web site, it should be shown on top and all other bumped down.

    A better approach is to default to the current behavior (since you seem attached to it) and give us an option to change the sorting.

  33. Carol

    Hi Dustin,

    I love your widget but I have a question. When I put it on my Blogger site, the header is too tall. Is there any way to fix this? I’ve tried changing the dimensions but it doesn’t change the header.

    Thanks so much,
    Carol

  34. Jaco

    Hi

    How can I display multiple profile widgets on the same page. I have tried changing the div id for each , but it is still not working.

    Thanks
    Jaco

  35. Karthik Murugan

    Also, change this line of code,


    this.results = this.results.slice(0, this.rpp);

    with


    this.results = this.results.slice(0, this.rpp).reverse();

  36. Dustin Diaz

    karthik,
    you might think that’s better, but it’s not. really.

  37. Natalia

    I like this widget quite a lot. I’m using it for a course web site–my first experiment in using Twitter for teaching.

    The only problem is that the widget doesn’t seem to be updating. I had some problems relating to the Blogger template I’m using — it ate up part of the code, but a friend helped me fix that.

    There should be three tweets showing up currently, and one is not appearing. It does show up if you do the search directly through Twitter. It is not a RT.

    I’m mystified, and my assiduous Googling has turned up nothing in the way of troubleshooting. I would love some help, if possible.

  38. Chris SPicks

    We cannot get the twitter widget to work at all. We can get twitstat that Ryan McGrath posted to work fine, but his wont scroll though results.

    Is there some other code that we “should” have in our site to make this work? Sent your code to our webmaster and he swears taht it is missing some code or something. We just get “emptyness” no box, no color, nothing.

  39. Karthik Murugan

    yes, I know. But I had to do that for my customer :)

  40. Craig Hicks

    I am setting up the search widget for our web site, but for some reason it will not display any results for

    search: ‘from:ncrr_nih_gov’,

    Is this an issue with my code, a setting that I need to change in Twitter to allow the ncrr_nih_gov tweets to be discoverable by the search, or something else entirely?

    Thanks for your consideration,

    Craig

  41. Craig Hicks

    Hi Dustin,

    I am setting up the search widget for our web site, but for some reason it will not display any results for

    search: ‘from:ncrr_nih_gov’,

    Is this an issue with my code, a setting that I need to change in Twitter to allow the ncrr_nih_gov tweets to be discoverable by the search, or something else entirely?

    Thanks for your consideration,

    Craig

  42. Parminder

    Is there a way to change the colors of the Myspace Twitter Widget? If not, can the HTML version of the diget be modified to include the reply, mark as favorite buttons like the Myspace widget?

  43. Craig Hicks

    Sorry for all the redundant posts. My browser kept returning error messages that suggested my comment was not posting.

  44. sinisterfrog

    Location based search operators don’t seem to be working (ie. “near:” or “within:”) Is this another bug?

  45. Kim Conley

    I have the profile widget on our site and I have the loop set to false but it is still showing old tweets. Any suggestions?

    Thanks!
    Kim

  46. Todd

    Dustin - This is a great widget…and no good deed goes unpunished. We’d love to use this on our Public Safety website, but don’t want an update on a fire from 10 days ago showing up.

    Know it’s been asked previously, but a feature to disable scrolling and only show X most recent tweets would be great.

    Thx

  47. Heather

    Hi, Dustin! Even though my code reads false for looping, the widget continues to scroll through old tweets. Can you help me? Thank you in advance!

  48. Nick Fedoroff

    Hi Dustin,
    Love the work you’ve done here. I’m having a similar issue to Jaco’s. I’m trying to display two search widgets on the same page. The widgets appear just fine, but all of the tweets show up in the second widget. I gave them unique id’s, matching the div id’s, but no dice.

    Thanks,
    Nick

  49. curtis

    I love the Twitter Search Widget so I have spent some time trying to get it to work for me. The problem I am having is when I put the widget in an iframe on my page,then when a link is clicked in the widget the webpage that opens is opened INSIDE the widget. I need it to open in a new win dow OUTSIDE the widget. I have tried adding Target=_”blank” to the href with no luck. Can you help me? Like I said, i really like this widget and have spent hours trying to find a soultion so i could keep it..in fact, its 3AM here and Im still looking…pls help

  50. Chris Evelo

    How can you limit the number of tweets? I would only like to show the last tweet.

  51. Chris Evelo

    How do you show the tweets in the last 3 days?

  52. Sharon

    Hi Dustin,

    I’ve got a widget pulling in multiple “from:” searches, however, I want to display the username but not the avatar. It seems that when I set avatares: false, it also hides the username.

    Is there a feature to show username but not avatar?

Get "JavaScript Design Patterns"

"As a web developer, you'll already know that JavaScript™ is a powerful language, allowing you to add an impressive array of dynamic functionality to otherwise static web sites. But there is more power waiting to be unlocked--JavaScript is capable of full object-oriented capabilities, and by applying OOP principles, best practices, and design patterns to your code, you can make it more powerful, more efficient, and easier to work with alone or as part of a team."

Buy JS Design Patterns from Amazon.com Buy JS Design Patterns from Apress

All content copyright © 2003 - 2009 under the Creative Commons License.

Archives | Blog Search