Add a Facebook Fan Widget Using Valid Code

February 1, 2010

The code that Facebook provide for adding their Fan Page widget to external sites is not valid for the XHTML 1.0 Strict doctype. Darcy found this out as she attempted to make all of the code on her site valid, and only the widget code remained invalid for the doctype that her Thesis theme uses.

I played around with some code, and came up with something that works in all browsers tested so far, and also passes validation.
It has been tested with the following browsers:

  • Firefox 3.5.7, 3.6
  • IE 6, 7, 8
  • Opera 9.64 (and assumed to work on later versions)
  • Chrome 4.0.249.78
  • Safari 4.0.4

If you find it works in another browser / browser version, please leave a comment and I’ll add it in.

The Code

<!--[if !IE]><!-->
    <object data="http://www.facebook.com/connect/connect.php?id=94957159371&amp;connections=12&amp;stream=1" type="text/html" class="facebook_fans">
        <p class="facebook_fans_link"><a href="http://www.facebook.com/graphicallydesigning">Graphically Designing</a> on Facebook</p>
    </object>
<!--<![endif]-->
<!--[if IE]>
    <iframe src="http://www.facebook.com/connect/connect.php?id=94957159371&amp;connections=12&amp;stream=1" class="facebook_fans" allowtransparency="true" frameborder="0"></iframe>
<![endif]-->

To make this show your fan page instead of Darcy’s, you’ll need to do a couple of things:

  1. Change the numerical id value to the one for your own page. This needs changing in two places in the code.
  2. Amend the fallback text (in the code above it’s a paragraph with a link) to whatever you want (see below).

The Breakdown

In short, by using conditional comments, that says to give non-IE (Internet Explorer) browsers an object and IE browsers an iframe. As the W3C Validator falls into the first category, then it doesn’t see the iframe that’s invalid for the Strict doctype, and so the code passes validation. Lets look at each main section:

<!--[if !IE]><!--><object data="http://www.facebook.com/connect/connect.php?id=94957159371&amp;connections=12&amp;stream=1" type="text/html" class="facebook_fans">…</object><!--<![endif]-->

In this snippet, the middle section has been removed and replaced with . The conditional comments are only understood by IE, and in this case, simply tell IE to ignore the whole section. For embedding stuff like movies, usually a classid attribute would be added to the object, but as this is plain text/html we’re bringing in, then the easiest solution is tell IE to skip straight over it completely. That leaves all other browsers with strict valid code.

<p class="facebook_fans_link"><a href="http://www.facebook.com/graphicallydesigning">Graphically Designing</a> on Facebook</p>

We add in some fallback content so that if a browser can’t display the object for any reason, then the fallback content would display instead. Here’s it’s a normal paragraph with a link. If you want to pass automated accessibility tests, then providing fallback content is required.

<!--[if IE]><iframe src="http://www.facebook.com/connect/connect.php?id=94957159371&amp;connections=12&amp;stream=1" class="facebook_fans" allowtransparency="true" frameborder="0"></iframe><![endif]-->

Again we use conditional comments, but this time we’re only focussing in on IE. We use an iframe in a similar method to the object in the first section. Although the iframe is not valid in XHTML 1.0 Strict, it doesn’t matter; all user-agents, including the W3C Validator still think we’re in one giant HTML comment, so it’s all ignored – only IE is paying any attention, so the page validates.

The Styles

My demo uses the following styles:

.facebook_fans {
  border: none;
  width: 220px;
  height: 720px;
  background: transparent;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.facebook_fans_link {
  font-size:0.8em;
  margin: 0;
  padding: 0;
  padding-left:10px;
}

The first section styles the object, making it a certain width or height (you’ll probably want to change these) and removing any inherent margin and padding your site may give to the object. The second section is for the fallback text, and can probably be ignored for the most part.

Live Demo

Bonus Tips

If you add the code above to something like a WordPress Post or Page (rather than to a template it may use), and find yourself with extra spaces, then try removing the line-breaks in the code – in testing this demo I found WordPress was automatically adding in paragraph elements as it usually does.

The number after the connections= bit in the code (twice) allows you to choose how many of your fans to show in the bottom section. Changing the number after the stream= from 1 to 0 allows you to turn the Wall Posts top section off. Changing either of these may mean you’ll want to change the height value in the CSS.

Have you found this useful? Did it work? Any problems? Let me know!

Update 2010-02-20: Amended the code snippets to ensure the & are displayed correctly as &amp;.

{ 3 comments… read them below or add one }

Clark February 19, 2010 at 21:28

Thank you very much for this tutorial. It validated for me in Dreamweaver CS4, however it did not validate with the W3C.

I just had to change both (two) of the ‘&’ to ‘&amp;’ in line 2 of your code. After I did that everything worked just fine (at least in Firefox 3.6). Thank you again for your help

Gary February 20, 2010 at 11:33

Hi Clark, thanks for confirming it works.

For the same reason that you found your comment automatically got converted from &amp; to &, my code had also been converted! I’ve amended both now, to ensure everything validates correctly.

Thanks :)

Siddhu April 23, 2010 at 09:01

I used the first part of the code on my site.. It got validated xHTML Strict :D This is amazing :D Never thought this would be possible… Thanks a lot for sharing this…

Leave a Comment

Previous post:

Next post: