- Progressively Enhanced Flash With SWFObject
- Passing So You Don't Fail
- Conclusion
Passing So You Don’t Fail
With just a little modification to the above example you can easily start sharing content between the two mediums.
<script type="text/javascript"> var so = new SWFObject("slideshow.swf", "mymovie", "400", "200", "8", "#336699"); so.addVariable('xmlData', encodeURIComponent(document.getElementById('flashcontent').innerHTML)); so.write("flashcontent"); </script>
SWF object has a very handy method called addVariable(), which—as its name suggests—lets you pass all sorts of content into your SWF. As you’ll quickly learn if you maintain a Flash website that houses all of its content within the SWFs, it can be a bit of a pain in the keester to keep up to date. Popping open an HTML file is faster, and—as we humans tend to be a bit lazy at times—fast updating helps ensure your content actually stays fresh.
Inside the addVariable() method, the contents of <div id=”flashcontent”> are cleverly retrieved with a little JavaScript, which encodes it to ensure a safe journey into the SWF.
Once your content has arrived in your SWF, you’ll need to use a little ActionScript to receive it and get it on stage. On the first frame of your Flash file, add the following code:
var xml = new XML(); xml.ignoreWhite = true; xml.parseXML(unescape(_root.xmlData));
Flash’s XML object handles the parsing. To use it, you simply create a variable in which you store a new instance of the object, make sure the parser skips over whitespace, then parse the HTML passed in.
To show the content on the Stage, use the Type tool to create a text box as shown in Figure 3.
Figure 3 Draw a text box on the Stage to display your HTML content.
Name the instance “display” in the Properties inspector.
Figure 4 Define the instance name for your text box in the Properties inspector.
To wrap up the display of the HTML content, we just need to add one final line of code to that script on frame one. This last line targets the “display” instance, and converts the parsed content to a string so it can be shown on stage.
display.text = xml.toString();
You’ll probably need to do a little extra cleanup of your content when it comes into Flash, but as that’s beyond the scope of discussion here, I’ll leave that as an exercise for the reader.