Embedding Flash Files Without Breaking Validation

Today I finally ran this new site through the W3C validator, and found that I had all kinds of red exclamation points. Some of the errors were simple oversights on my part, but most of them revolved around my header, which is an embedded flash movie.

Ever since IE7 sent web developers into a panic by breaking the object and embed tags (then fixing them again), I’ve been using Macromedia’s AC_RunActiveContent.js to render flash movies. The problem is, the script writes unnecessarily ugly code that doesn’t validate as XHTML 1.0 Transitional, so it kind of makes me look like a chump.

While searching for a solution today, I found Drew Mclellan’s brilliantly simple, five-year-old Flash Satay method, which works really well for my purposes (a small flash movie that doesn’t need to stream). To accommodate IE7 users who haven’t updated their browsers yet, I wrote my own very simple JS function that doc.writes the necessary object tag. It looks like this:

function make_flash(swfPath, swfWidth, swfHeight)
{
var str = ‘<object type=”application/x-shockwave-flash”‘;
str += ‘data=”‘ + swfPath + ‘”‘;
str += ‘width=”‘ + swfWidth + ‘” height=”‘ + swfHeight + ‘”>’;
str += ‘<param name=”movie” value=”‘ + swfPath + ‘” />’;
str += ‘<param name=”allowScriptAccess” value=”always” />’;
str += ‘</object>’;

document.write(str);
}

Much, much simpler (but, admittedly, less flexible) than the Macromedia code. In the spirit of thoroughness, I also created a straight HTML version of the header for users with Javascript turned off.

I also learned another strange lesson while trying to get this seemingly simple flash header to work: if the user visits you at, say, http://angryrobots.com, but the link in your flash movie points literally to http://www.angryrobots.com, flash will see this as a cross-domain transaction, and won’t execute the getURL command. It doesn’t throw an error either–it simply doesn’t work. This is an issue for me, because I have two domains pointing at this site, and I can’t control whether or not users will type in the "www". So to combat this weird behavior, I had to throw in the "allowScriptAccess" parameter with a value set to "always". Lesson learned for next time, I guess.

Anyway… if you have another simple, flexible, standards-compliant method of embedding flash movies, please shoot me an email. I’d love to hear it.

Leave a Reply