Recipe 21.4. Passing Parameters to Flash from HTML


Problem

You want to pass parameters from HTML to Flash when the Flash content loads into the player.

Solution

Use the flashvars parameter/attribute with the <object> and <embed> tags.

Discussion

Frequently, you may want to pass some value from the HTML document to the Flash content when it loads. For example, you may have previously prompted the user for his name from an HTML form, and you may want to pass that to the Flash content. You can pass parameters from HTML to Flash content when the Flash content loads using something called flashvars. The flashvars parameter for the <object> tag and the flashvars attribute for the <embed> tag enable you to pass data from HTML to the Flash content. The value of the flashvars parameter/attribute needs to be in URL-encoded format. That means that the value looks like the value that normally composes a query string in a URL. The value can be one or more name-value pairs in which each name and value is delimited by an equals sign, and each pair is delimited by an ampersand. The following is an example of a URL-encoded string that defines two name-value pairs:

 first_name=Joey&last_name=Lott 

The basic syntax of <object> and <embed> tags is discussed in Recipe 21.1, and you can review that recipe if any part of the basic syntax in the following code is unfamiliar to you. The emboldened sections of the code are an example of the additional code that defines the flashvars parameter and attribute. In the example, two parameters are being passed to the Flash contentfirst_name and last_name:

 <object class codebase="http:// fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%"  align="middle">   <param name="allowScriptAccess" value="sameDomain" />   <param name="movie" value="example.swf" />   <param name="quality" value="high" />   <param name="scale" value="noborder" />   <param name="bgcolor" value="#FFFFFF" />   <param name="flashvars" value="first_name=Joey&last_name=Lott" />   <embed src="/books/2/703/1/html/2/example.swf" quality="high" scale="noborder" bgcolor="#ffffff" width="100%" height="100%" name="example" align="middle" allowScriptAccess="sameDomain" flashvars="first_name=Joey&last_name=Lott" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/ getflashplayer" /> </object> 

When the Flash content loads, the parameters are passed to the .swf as variables on the main timeline. For example, the .swf from the preceding example can reference variables called first_name and last_name on the main timeline.




Flash 8 Cookbook
Flash 8 Cookbook (Cookbooks (OReilly))
ISBN: 0596102402
EAN: 2147483647
Year: 2007
Pages: 336
Authors: Joey Lott

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net