Valid XHTML Strict Twitter JavaScript Code
20 May 2008
Twitter has become ever so popular and everyone is rushing to start tweeting, if they have a blog, they would want it to be displayed. However if you actually cared about having a valid XHTML site, you would have noticed that the code provided by Twitter has a couple of errors.
I will cut to the chase and give you the valid code.
The valid Twitter code
<div id="twitter_div">
<h2 class="twitter-title">Heading title</h2>
<ul id="twitter_update_list"><li></li></ul>
</div>
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/Username.json?callback=twitterCallback2&count=Number of tweets to display"></script>
Replace the Header title and Number of tweets to display with your desired value.
Here is what has been changed from the original code:
- A <li> tag has been added within the unordered list
- The ampersand (&) has been replaced with its character code (&) to fix the validation errors
Now you can display your tweets without breaking your site’s validation. Hope this helps you!
Ooh nice.. thanks! I never use Twitter before though. Maybe I should try..
Thank you very much for this post…this saved me some time tonight. One additional note for improving performance, you can place the two “script” lines at the very bottom of the section of your page. That way, your page will load first, and then it will ping twitter for your latest tweets. If twitter is down, at least your users will be able to see the rest of your site.
Thank you for the mention.
More specifically, place the 2 script tags just before the </body> tags.
I was working on getting my blog Valid XHTML. Now I am done. Thanks for the twitter & & tip. There are actually a whole lot more buttons out there that mess up your validation with these…
Congrats on a job well done. I noticed you are using XHTML Transitional. Why would you not want to use XHTML Strict?