SWF/Flash Embed code via JavaScript (with alternative content option)

by leon on October 10, 2007

Bit of a big title there, needed to summarise exactly everything that this script does. Here is a more detailed run down:

  • Embeds Flash into a html page using JavaScript – this gets around the fact that you have to ‘click to activate’ in IE7.
  • Caters for users that don’t have the correct player version – will prompt them to upgrade
  • Caters for users that don’t even have Flash – will display alternative content. e.g. a static image or alternative navigation – or anything at all that you want.
  • Is clean and easy to implement – the code is fully commented within the example.


Implementation

This code makes it easy to implement Flash into your site, while still catering for users that do not have Flash.
It makes use of JavaScript to embed the flash, via the included SWF object script. If the user has neither Java enabled or the Flash player
then they will see whatever is within the designated tags. You can change this to integreate into your site.
If you do change the ‘content’ div tag, ensure that the so.write variable is changed accordingly. (this will make sense when you see the file)

To include your Flash file, add the correct variables to code below noting that:

file_path.swf – must be the full path, including the .swf at the end
file_name – is only the name of the file, do not add the path, or the .swf at the end
width – the width of your flash movie
height – the height of your flash movie
player_version – the flash player your are targeting, e.g. 8. enter the lowest number you are targeting
background_colour – the background colour of your flash movie

If you need to add and FlashVars, these can be added like so:

so.addVariable(“variable_name”, “variable_value”);

If your need to add a parameter such as wmode, this can be added like so:

so.addParam(“wmode”, “transparent”);

These values should be added before the ‘so.write’ line.

The source files can be downloaded from here.

Hope you find this useful

Leave your comment

Required.

Required. Not published.

If you have one.