Sunday, December 13, 2009

Converting Google Picasa's embedded slideshow code to valid XHTML/HTML

Defining the Problem:

You have created a picture album in Google Picasa and want to embed a slideshow of that album in your website or blog. Fortunately, Google Picasa makes it easy to generate the code to do so. All you have to do is copy and paste the generated code into the HTML of your site.

Here's an example of that generated code, using my Chinese Zodiac desktop wallpaper album:

<embed type="application/x-shockwave-flash" src="http://picasaweb.google.com/s/c/bin/slideshow.swf" width="144" height="96" flashvars="host=picasaweb.google.com&hl=en_US
&feat=flashalbum&RGB=0x000000&feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2FJonah.Chanticleer%2Falbumid%2F5220445689687444257%3Falt%3Drss%26kind%3Dphoto%26hl%3Den_US" pluginspage="http://www.macromedia.com/go/getflashplayer">

</embed>

Unfortunately, Picasa's generated code uses the <embed> tag, which is not part of the HTML/XHTML standards (see http://validator.w3.org/docs/help.html#faq-flash for more information). Furthermore, the value assigned to the flashvars attribute of the <embed> tag has multiple ampersands in it, and those ampersands are not properly URL-encoded (see http://www.htmlhelp.com/tools/validator/problems.html#amp for details). This means the code will break validation for any page into which you paste it. Many people don't care if their pages fail to validate, but since you've made the effort to find this article and read this far, I'm assuming you are a web development professional who does care if your page throws a web-browser into "quirks mode."(http://en.wikipedia.org/wiki/Quirks_mode)

Creating a Solution:

To regain proper validation, we need to address the two following issues:

1. Switch from <embed> to <object>, which is part of the HTML/XHTML standards

2. Properly encode the ampersands by changing them from "&" to "&amp;"

Switching from embed to object:

Let's take a closer look at the attributes of the <embed> tag example from above. I've separated them by linebreaks to make them easier for us to read:

<embed

type="application/x-shockwave-flash"

src="http://picasaweb.google.com/s/c/bin/slideshow.swf"

width="144"

height="96"

flashvars="host=picasaweb.google.com&hl=en_US&feat=flashalbum&RGB=0x000000&feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2FJonah.Chanticleer%2Falbumid%2F5220445689687444257%3Falt%3Drss%26kind%3Dphoto%26hl%3Den_US"

pluginspage="http://www.macromedia.com/go/getflashplayer">

</embed>

The good news is that three of the six attributes (width, height and type) translate directly into attributes with the same names for the new object tag. We are off to a good start.

<object type="application/x-shockwave-flash" width="144" height="96">
<!-- TBD -->
</object>

By reading the HTML reference page for the object tag (http://www.w3schools.com/TAGS/tag_object.asp), we can figure out that the src attribute doesn't exist-- the closest equivalent to it is the data attribute. The two remaining attributes, pluginspage and flashvars, do not have any counterparts in the attributes of the object tag. We will need to pass them directly as run-time parameters with the param tag (http://www.w3schools.com/TAGS/tag_param.asp).

It is worth noting this one important note from the object tag reference: "The object support in browsers depend on the object type. Unfortunately, the major browsers use different codes to load the same object type." In other words, all standards-compliant web browsers will recognize the object tag, but they may do so in different ways. Netscape-family browsers might respond to the data attribute, while Internet Explorer will not see that same information unless it is passed along in a param value.

Fortunately for us, Drew McLellan's article in the Nov. 9 2002 issue of A List Apart, "Flash Satay: Embedding Flash While Supporting Standards," gives us just the perfect trick to make the object/param sandwich that works with both sets of browsers. Check it out below:

<object type="application/x-shockwave-flash" width="144" height="96" data="http://picasaweb.google.com/s/c/bin/slideshow.swf">
<param name="movie" value="http://picasaweb.google.com/s/c/bin/slideshow.swf" />
</object>

(Mr. McLellan, if you should find yourself in the DC Metro area, drop me a line-- because your first beverage is on me. This bit of brilliance saved me time and a headache!)

Let's stay on track-- because we haven't finished just yet! We still need to create param tags for the pluginspage and flashvars, and nest them between the object tags.

<object type="application/x-shockwave-flash" width="144" height="96" data="http://picasaweb.google.com/s/c/bin/slideshow.swf">
<param name="movie" value="http://picasaweb.google.com/s/c/bin/slideshow.swf" />
<param name="FlashVars" value="host=picasaweb.google.com&hl=en_US&feat=flashalbum&RGB=0x000000&feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2FJonah.Chanticleer%2Falbumid%2F5220445689687444257%3Falt%3Drss%26kind%3Dphoto%26hl%3Den_US" />
<param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer" />
</object>

Encoding the ampersands:

Almost done. There's only one thing left to fix-- those pesky ampersands assigned to the "value" attribute of the param tag named "FlashVars." This is literally a case of substituting "&amp;" for "&", as I've done below (bold removed for emphasis):

<param name="FlashVars" value="host=picasaweb.google.com&amp;hl=en_US&amp;feat=flashalbum&amp;RGB=0x000000&amp;feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2FJonah.Chanticleer%2Falbumid%2F5220445689687444257%3Falt%3Drss%26kind%3Dphoto%26hl%3Den_US" />

Final Result:

So, our end result should look like this when we view it as an entire page (assuming XHTML 1.0 Transitional):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>. . .</title>
</head>
<body>

<!-- any content in your page that appears before the slideshow -->

<object type="application/x-shockwave-flash" width="144" height="96" data="http://picasaweb.google.com/s/c/bin/slideshow.swf">
<param name="movie" value="http://picasaweb.google.com/s/c/bin/slideshow.swf" />
<param name="FlashVars" value="host=picasaweb.google.com&amp;hl=en_US&amp;feat=flashalbum&amp;RGB=0x000000&amp;feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2FJonah.Chanticleer%2Falbumid%2F5220445689687444257%3Falt%3Drss%26kind%3Dphoto%26hl%3Den_US" />
<param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer" />
</object>

<!-- any content in your page that appears after the slideshow -->

</body>
</html>

Extra Credit:

Now that your page validates, you can congratulate yourself on a job well done-- until the next time you need to create a new slideshow code snippet in Picasa. Or someone else tries to use the invalid slideshow code on their webpage, and asks you to help them fix it. Or doesn't ask you to help them fix it.

The truth is what I've shown you here is only a band-aid, not a cure for the real problem. There are many tools out there which generate invalid HTML code snippets; Google's Picasa is only one of them. If you care about web standards and want to see them realized on the World Wide Web, you need to contact the developers responsible for popular tools like Picasa (http://www.google.com/support/forum/p/Picasa/thread?tid=3ca39d2989c708ac&hl=en) and persuade them to make changes that will result in the output of valid HTML code. Be patient and polite when you advocate for these changes, not pushy and critical. You never know the circumstances behind the development of an application or product. It may be the developer created the code before a specific standard had been fully ratified, or the developer may have been forced to use third party libraries/components that generate invalid code. By advocating and educating in a positive manner, you can give a developer the motivation, desire and information they need to do the right thing.

Acknowledgments/Further Reading:

Thanks to E.G. for bringing this whole Google Picasa puzzle to my attention in the first place.

Drew McLellan, "Flash Satay: Embedding Flash While Supporting Standards", A List Apart, Nov. 9, 2002, http://www.alistapart.com/articles/flashsatay

Picasa Web Albums Help Forum (http://www.google.com/support/forum/p/Picasa/label?lid=051816230f0ec560&hl=en)

9 comments:

Anonymous said...

Thank You so much. Like I said I almost went crazy trying to figure out how to do that.
Sadly I didn't even realize what I was seeing. I looked through the validator's response and saw the ampersand remarks, but just couldn't clue in.
I have linked to Your site to thank You.

silysavg said...

Jonah, Thanks so much for your article. Until I found your article I spent about 1 hour reading various other articles about the 'embed' code causing problems for XHTML validation. The others were much too complicated. Yours had just the right mix of technicality and brevity. After reading your article it was a matter of copy and paste your sample to my web page, then substitute my specifics, as they relate to my Picasa/Google account plus the width and height attributes and BAM! it worked. It was so easy in fact that I was sure that when I clicked the 'Validation' icon at the bottom of the page it wouldn't validate - BUT IT DID. AND it works great! If you send me a note I would be glad to donate a few bucks to your Paypal account should you have one.
Thanks again, Mike Sillett( silysavg@gmail.com ). P.S. no page to look at yet as I am about three days away from launching my second site.

Jonah Chanticleer said...

Hi, Mike. Thanks for the generous feedback; I'm glad an entry I wrote was able to help someone else. My current work situation prevents me from accepting your offer re: Paypal, but perhaps you'd be willing to make a charitable donation to an organization such as the American Diabetes Association, instead?

Drop me a note when your new site debuts. I'd like to see it.

Unknown said...

Jonathan you seem to know a thing or two about embedding picasa slide shows unto a web site so let me pose a question to you, the answer to which has alluded me for some time. I am a volunteer web master for my daughter's elementary school web site .. every time there is a school event I get several request to add photos taken by parents to the web site, almost always they are sent to me as picasa photo albums. I would love to be able to generate the html code to embed without involving the parents. Is there a way to do this?

Jonah Chanticleer said...

Hello, Guy

Let me make sure I'm understanding your question. The parents are attending school events, taking pictures with their digital cameras, uploading them to Google Picasa and then sending them to you to be included on the school website. You are happy with the quality and quantity of the actual pictures, but want to use HTML on your website that is completely independent of Picasa albums, Flickr, or whatever photo sharing service the parents might be using. Correct?

By the way, my name isn't Jonathan-- it's Jonah. No worries, it was an honest mistake. :)

Unknown said...

Thank you for saving me hours! I am a mew web designer and want to be able to "do it right" for my clients. Appreciate you posting this!

Jonah Chanticleer said...

You are quite welcome, Misty. I'm glad you found it useful. Good luck in your new career as a web designer, and keep in touch. I hope you will also consider checking out my irregularly published series "From Red To Green" over at jonahchanticleer.com.

drksmth said...

Thanks a lot! This is exactly what I was looking for. Your article is very well-written as well (always a plus)!

Jonah Chanticleer said...

A quick addendum to this little article-- the embed tag has been included as part of the HTML5 spec, which means if you are writing an HTML5 document with embed in it, and run it through an HTML5-specific validation tool, it should *not* throw an error or warning.

May we live in interesting times, right? ;)