Embed a Valid XHTML Twitter Widget

Recently, I decided to embed the flash twitter widget in my blog. In general, I don’t like to use flash because it’s a proprietary format, but looks like it’s becoming a de-facto standard and it’s not easy to cover its functionality with open source alternatives.

Embedding the twitter widget should be easy, just to copy-past a short HTML code, and that’s it.

And this is what I did, and it worked. But, when I tried to validate my website’s XHTML, I got errors:

The supplied code uses the “object” tag, which is non-standard and therefore not XHTML compliant.

I have a few choices:

The easiest is to accept having a non XHTML compliant website: Not me, I’ll never do this consciously.

Another option was to use a non-flash widget: Actually this is what I did till now, it’s simple and nice, but flash looks much better.

So, I’m left with the only option of fixing the code, which was, in this case, a very simple task.

I won’t explain the functionality of each tag, since I’m not a HTML expert. I’ll just give an “algorithm” to fix the code, so I/You can use it as a reference for later times. If you want to learn more about HTML tags, this is not the place, at least not for now.

Here we go:

When you choose the flash widget from Twitter, you’ll get a code similar to this:

1
2
3
4
5
6
7
8
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,124,0"
width="290" height="350" id="TwitterWidget" align="middle"/><param name="allowScriptAccess" value="sameDomain"/><param name="allowFullScreen" value="false"/><param name="movie" value="http://static.twitter.com/flash/widgets/profile/TwitterWidget.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#000000" /><param name="FlashVars" value="userID=16738995&amp;styleURL=http://static.twitter.com/flash/widgets/profile/smooth.xml"/><embed src="http://static.twitter.com/flash/widgets/profile/TwitterWidget.swf"
quality="high" bgcolor="#000000" width="290" height="350"
name="TwitterWidget" align="middle" allowScriptAccess="sameDomain"
pluginspage="http://www.macromedia.com/go/getflashplayer"
FlashVars="userID=16738995&amp;styleURL=http://static.twitter.com/flash/widgets/profile/smooth.xml"/>
</object>

The problematic part is the “embed” thing, so, first of all, we’ll Remove the embed tag.

Now, the code is XHTML valid, but unfortunately it doesn’t work in good (non IE) browsers.

We’ll use nested object tags to fix this:

After the first object tag, we’ll add this:

1
2
3
4
<!--[if !IE]>-->
<object type="application/x-shockwave-flash"
data="http://static.twitter.com/flash/widgets/profile/TwitterWidget.swf" width="240" height="350">
<!--<![endif]-->

The comments are actually part of the code, they tell IE to ignore this, so this tag is applicable only in better browsers (Firefox, Opera, etc..)

Of course, we’ll need also to close this tag, so we’ll add this one line before the closing object tag:

1
2
3
<!--[if !IE]>-->
</object>
<!--<![endif]-->

Last thing is to replace all instances of & with &amp;

That’s it, now every thing should be fine.

P.S. : This “trick” is useful for other cases when you embed flash. I’m not sure how it works with large flash files, this is something that needs deeper investigation.


If you enjoyed this post, make sure you subscribe to my RSS feed!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Related posts:

  1. Fetch Links From Twitter Into Your RSS Reader
  2. Embed A Document Using Google Docs

Tags:

Thursday, February 19th, 2009 Web Development

9 Comments to Embed a Valid XHTML Twitter Widget

  • Simon says:

    Your code is not actually XHTML compliant, you only let the W3C checker bypass the code. That’s not an actual solution.
    A real XHTML1.1 strict code can be found on my site. It works as far as I know, although I think the twitter-widget is a bit sluggish on my site sometimes (might be my server?)

  • Amir Watad says:

    Simon,
    First of all, thank you for your comment.
    Second: I’m quite sure that my code is XHTML valid. If you claim otherwise, you maybe want to give a better explanation that just saying “it doesn’t work”.
    Third: Your site is not English, so it cannot help me very much.
    Anyway, I’m not using the above method anymore as it has issues. I’m using SWFobject for this, I’m just too lazy to post about it, but a post will come soon.

  • Simon says:

    My site is english, there’s a button in the upper right ;)

    But, I’m not claiming your solution is not XHTML, but I say, that from what I see, it’s a partly work-around for the problem, although it does work fine, don’t worry about that :)

    I’ve changed my URL to the english version should’ve done that before.
    Your solution takes note of IE, my solution isn’t browser dependant anymore.
    Or I misread something, and your solution is the same, but then I’m missing something.
    For easy url to my solution:
    http://www.casa-laguna.net/taal/view/95

  • Mr. Radio says:

    Actually this fix was fairly useful for me, I’m writing a whole new version 2.0 of my site and i want it to be xhtml and use this twitter widget.

    So yeah, thanks for the fix – it works for me (I’m not a great coder or anything…but just wanted to give a big thank you!) and it’s going to appear in the version 2.0 of my site that I plan on launching within a month or so.

  • Amir Watad says:

    Thank you @Mr. Radio for your comment.
    I’m happy that you found this useful.
    However, I don’t like this method for embedding flash. It has disadvantages. (In the case of the twitter widget, you cannot login when you embed it using this method).
    The best way so far is using “swfobject”. http://code.google.com/p/swfobject/
    I use it here in this website. Maybe I’ll post about it when I’ll have a little free time.

  • Me says:

    Sorry to tell ya that Simon was right, it’s good that you want to make your sites valid and that you only will accept a valid site, I am the same way, but Simon is right in that fact that yes your code does validate but your code is only validating based on the fact that your tricking the validator into thinking its valid. Of course sometimes its unavoidable to where you have to use a seperate ie stylesheet to fix all those annoying problems with ie6, but in a general rule its best to avoid this types of fixes to truly have a valid website. Check out Simons link for the proper solution to fix this and actually the widget runs much quicker.

  • Me says:

    By the way your captcha thing sucks it doesn’t work right every time, I had to input the same captcha code 3 times in a row to make it work. Try using CFormsII for your form needs, its the best out there for wordpress.

  • Tweetbacks

    Leave a Reply

    my email
    my photo
    Hi,
    My name is Amir Watad. I have a BSc. in biomedical engineering from The Biomedical Engineering school , Technion , Israel, and a BSc. in electrical engineering from The Electrical Engineering school , Technion , Israel.
    I'm a Verification Engineer in Mellanox Technologies Ltd.
    I love Linux, the Command Line and the OpenSource Community.
    I used to write Poems (Arabic) when I was able to find time for this.
    February 2009
    S M T W T F S
    « Jan   Mar »
    1234567
    891011121314
    15161718192021
    22232425262728
    SEO Powered by Platinum SEO from Techblissonline

    Twitter links powered by Tweet This v1.7.3, a WordPress plugin for Twitter.