Parse Querystrings Using SWFObject

Sometimes you may need to pass querystring parameters to a SWF on page load. This can be accomplished quite easily using server-side scripting such as PHP or ASP.

Notes:

  1. SWFObject 2.x has both static and dynamic publishing options, each requiring a different approach to flashvars.
  2. The following examples do not protect you against malicious querystring code. Please be careful in your implementations to protect yourself from malicious HTML injection.

Static publishing

The static publishing method requires adding flashvars as a param element in the object. Here's one way to do it using PHP:

<?php
   $flashvars = "";
   foreach($_GET as $variable => $value) {
      $flashvars .= $variable . "=" . $value . "&";
   }
   //Get rid of the last ampersand
   $flashvars = rtrim($flashvars, "&");
?>
<object id="flashcontent" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="550" height="400">
    <param name="movie" value="/swfobject/assets/sample.swf" />
    <param name="flashvars" value="<?php echo $flashvars ?>" />
 
    <!--[if !IE]>-->
    <object type="application/x-shockwave-flash" data="/swfobject/assets/sample.swf" width="550" height="400">
    <param name="flashvars" value="<?php echo $flashvars ?>" />
    <!--<![endif]-->
      <p>Please update your Flash Player</p>
    <!--[if !IE]>-->
    </object>
    <!--<![endif]-->
  </object>

Dynamic publishing

The dynamic publishing method allows you to use a JavaScript object to pass the variables.

<script type="text/javascript">
 
var params = {};
var attributes = {};
var flashvars = {};
<?php
foreach($_GET as $variable => $value) {
   echo "flashvars['" . $variable . "'] = " . "'" . $value . "'; \n";
}
?>
 
swfobject.embedSWF("file.swf", "flashcontent", "550", "400", "9", "expressinstall.swf", flashvars, params, attributes);
</script>

Questions or Comments?

If you have questions or would like to point out an error, please post your remarks in the SWFObject Google Group. Thanks!