There seems to be a lot of confusion about the code needed to properly embed Flash objects within a website: it's the most frequently asked question at this site's forum. The rise of social networking sites like Myspace (who filter lots of codes) and the Eolas patent enforcement have thrown quite some confusion into the mix. In this tutorial, I'll shortly align the best options to embed Flash.
Single embed code
Let's start with the easiest way to embed Flash in a website, with a single embed code. We presume we have a mediaplayer.swf file located in the same directory as the HTML page it's embedded in
<embed src="mediaplayer.swf" width="300" height="300" allowscriptaccess="always" allowfullscreen="true"/>
Of these parameters, the most important one is the src. This contains the location of the SWF file to include, here mediaplayer.swf. If the SWF file resides in another directory, we can point to it with a relative path (eg. ../flash/mediaplayer.swf or an absolute path (eg. http://www.myserver.com/flash/mediaplayer.swf).
The width and height parameters comply with the width and height of the SWF in pixels, but they can also be entered as a percentage of the SWF container (eg: width="100%").
Last, the allowscriptaccess and allowfullscreen parameters tell the browser the SWF can both communicate with external scripts (and link to external pages) and use the new fullscreen mode. If you do not want to allow this, you can remove both parameters, since they default to respectively sameDomain and false.
The code listed above uses only a couple of the available parameters. A complete list of them can be found at Adobe's website, but let's show the most interesting ones here:
- bgcolor (#rrggbb): set the SWF background color using a hexadecimal value.
- flashvars (variables): the variables placed here will be loaded in the SWF.
- menu (true, false): set this to false to hide most of the rightclick menu.
- wmode (opaque, transparent, window): set this to either transparent or opaque to fix z-index or flickering issues.
Of the above parameters, the most interesting one is flashvars, which allows you to send startup variables to the SWF. This site's FLV Media Player and Image Rotator support a large number of these variables, making them easy to customize and reuse. Let's give our example mediaplayer.swf three flashvars; one for the list to play, one for the background color and one for automatically starting the song:
<embed src="mediaplayer.swf" width="300" height="300" allowscriptaccess="always" allowfullscreen="true" flashvars="file=playlist.xml &backcolor=0x000000&autostart=true"/>
As you can see, flashvars are entered as a single string, with an = symbol separating the name and value and an "&" symbol separating subsequent flashvars. There's no limit to the number of flashvars you can add to the embed code. Once more note that the FLV Media Player and Image Rotator also support quite a few of them.
Three problems related to flashvars often cause them not to load. The first relates to relative linking of files. When linking to MP3, image and XML files, you should always start from the location of the HTML in which the SWF is embedded. So, in the previous example, if the SWF was to be put in a subdirectory, the flashvar pointing to the playlist would remain to be file=playlist.xml and not file=../playlist.xml! There's an exception: the path of FLV files should be given relatively from the SWF file. In order to prevent confusion, you can always point to files using an absolute URL (including the http://www part).
The second problem relates to the importing of XML files. Due to security restrictions, Flash can only import XML files if they reside on the same domain as the SWF file. So, in our mediaplayer example, if the SWF is located in the domain www.myserver.com the playlist.xml should also reside on www.myserver.com. A workaround for this is to use a simple crossdomain.xml file, which should be installed in the root of the site that contains the XML. Here's the one from YouTube.
The third problem relates to the three symbols ?, = and &. Since they are used to stack the flashvars, they cannot be used in the flashvars themselves. By escaping these vars, the problem can be solved. Therefore, replace the three symbols with their escaped strings and unescape() them again in the SWF:
- ? → %3F
- = → %3D
- & → %26
In 2005, a large embed problem arised after a small company named Eolas filed (and won) a lawsuit against Microsoft. Eolas enforced a patent on automatically embedding external applications (like Flash) in webpages. As a result, Microsoft updated Internet Explorer: you'll see a grey border appearing around an SWF when you move your mouse over it. Before interacting with it, you first have to click once to activate it. Very annoying.
A second problem with the regular <embed> code is that it's not a valid XHTML tag. So, if you want to build a standards-compliant, accessible website, you cannot use it.
Second, give the HTML element in which the SWF should be placed a unique id:
<div id="flashbanner">this will be replaced by the SWF.</div>
Third, instantiate an SWFObject below the named HTML element and put all parameters in there (for a full list of parameters, read the SWFObject Website):
The SWFObject instance will create the code needed to embed the mp3player.swf. It sets it's dimensions to 300 by 250 pixels and also sends the flashvars for the file, backcolor and autostart variables (with a nice, separate line per flashvar). Finally, the instance replaces the text by the SWF, so we can see a Flash movie directly embedded in our page, without borders and with XHTML validity!
The implementation of SWFObject wraps up this general overview of the embedding of Flash in websites. For questions or hints, or in case I've forgotten anything, please join this site's forum. If you want to create specific embed codes for the FLV Media Player or the Image Rotator, take a look at the setup wizard, which can automatically generate it.