Recipe 22.3. Passing Parameters from HTML


Problem

You want to pass parameters from HTML to an SWF.

Solution

Use FlashVars.

Discussion

FlashVars provides a solution by which you can pass parameters to an SWF from HTML. This can be very useful when you want to pass simple data to an SWF and the data is likely to change, depending on where the application is deployed. For example, you might want to pass web services URLs to an SWF when the URLs change, depending on the server on which the SWF is deployed. That enables you to redeploy the SWF with new values without having to recompile the SWF.

The FlashVars solution requires two parts: one part is implemented in HTML, and the other is implemented in ActionScript.

The HTML portion of the FlashVars solution requires adding a <param name="FlashVars"> tag to the <object> tag, and a FlashVars attribute to the <embed> tag. The value for the tag and attribute is a URL-encoded sequence of name-value pairs. For example, the following constitutes a valid FlashVars value that defines two name-value pairs, url1 and url2:

url1=http://www.example.com&url2=http://www.sample.com

Within ActionScript, you can reference the variables and values passed via FlashVars using the root.loaderInfo.parameters property of any display object. The root.loaderInfo.parameters property is an associative array, the keys of which are the names of the variables passed to the SWF via FlashVars. For example, using the preceding example value, the root.loaderInfo.parameters property would have two keys: url1 and url2.

Using FlashVars with JavaScript, you can pass a query string from the HTML page to an SWF. The following JavaScript illustrates how to write the <object> and <embed> tags with JavaScript so they pass the query string data from the HTML page to the SWF using FlashVars:

// Retrieve the query string, and assign it to a variable. var parameters = window.location.search.substr(1); var objectEmbed = '<object class  width="100%" height="100%" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'; objectEmbed += '<param name="movie" value="Example.swf" />'; objectEmbed += '<param name="quality" value="high" />'; objectEmbed += '<param name="bgcolor" value="#869ca7" />'; objectEmbed += '<param name="allowScriptAccess" value="sameDomain" />'; objectEmbed += '<param name="FlashVars" value="' + parameters + '" />'; objectEmbed += '<embed src="/books/2/701/1/html/2/Example.swf" quality="high" bgcolor="#869ca7" width="100%" height="100%" name="Example" align="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" FlashVars="' + parameters + '"></embed>'; objectEmbed += '</object>'; document.write(objectEmbed);

FlashVars is an important feature of Flash Player. However, it's important to use FlashVars correctly. FlashVars is not designed to be a universal mechanism for passing data to an SWF. FlashVars is only for simple data that is likely to change, depending on where the SWF is deployed. For more complex initialization data loading, use a URLLoader object to load data at runtime.




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

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