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
1 2 3 4 5 6 | <div id="twitter_div"> <h2 class="twitter-title">Heading title</h2> <ul id="twitter_update_list"> <li></li> </ul> </div> |
To ensure that your page loads even if the Twitter JavaScript is being wonky, place the these JavaScript includes just before the </body> tag:
1 2 3 4 5 6 | <body> <!-- Your site code --> <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&amp;count=Number of tweets to display"></script> </body> |
Replace the Header title, Username, Number of tweets to display with your respective values.
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!
Darran Low is a 22 year old sports enthusiast currently pursuing a Bachelor degree in Computer Science at the University of Adelaide and aspires to become a Web Developer in the near future. 


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?
Thanks for this… Simple added to the code and its now all valid.
Cheers
Brilliant thanks for this!
Hi, Thanks for sharing , I am going to put on my site