Sayfandan veya Tarayıcıdan Flash'a Parametre Aktarmak
İngilizce bir makaledir. Herhangi bir html sayfasından veya Url üzerinden Flash'a veri aktarmak için yapılması gerekenler.
Generic Flash objects typically need one or more parameters to function. When displayed via a web page, there are 3 main methods to accomplish this.
- FlashVars Parameters
- Calling Javascript functions via the ExternalInterface class
- Using the LocalConnection class to access flash popup windows
Normally, I then use these parameters to read a configuration file and/or to query a database and get more information.
This page covers what works, what doesn't, and presents some related notes.
ExternalInterface class - Calling Javascript | Called by JavaScript | IE Failures | fscommand | SetVariable
Multiple Flash Applications
URL Parameters
http://computer/path/CGI.exe?name1=value1&name2=value2 |
The help says that this same technique can be used with Flash movies.
<embed src="myApplication.swf?name1=value1&name2=value2" /> |
Parameters passed this way are accessed using
var s:String = stage.loaderInfo.parameters["name2"]; |
I could not get this method to work.
FlashVars Parameters
The basic html code (but without FlashVars) is automatically generated via
- File / Publish...
which also creates AC_RunActiveContent.js. Note that, when using the default settings, the Flash file, html file, and AC_RunActiveContent.js should be copied to the same server directory.
To pass parameters, modify the html file to add FlashVars to both the AC_FL_RunContent function
AC_FL_RunContent( ... 'FlashVars','name1=value1&name2=value2', ... ); //end AC code |
and the object definition
<object ... > <param name="FlashVars" value="name1=value1&name2=value2" /> <embed src="myApplication.swf" FlashVars="name1=value1&name2=value2" [many more parameters here] /> </object> |
Notice that the object definition requires 2 entries of FlashVars
- param is used by Internet Explorer
- embed is used by Netscape and Macintosh Internet Explorer
whereas AC_FL_RunContent only requires one and creates the correct format based on the detected browser type.
Parameters passed this way are accessed in AS3 using any of the following
var s1:String = stage.loaderInfo.parameters["name2"]; var s2:String = this.root.loaderInfo.parameters["name2"]; var s3:String = root.loaderInfo.parameters.name2; // reference |
A number of characters have special meanings and can not be directly entered in names or values ... these will need to be entered as hex values. The following are some of most common problem characters.
space %20 & %26 + %2B = %3D |
The following will pass a filename with query parameters. Without the hex encoding, this parameter would be broken up into several separate parameters.
<object ... > <param name="FlashVars" value="Filename=Delphi_Program.exe?name1%3Dvalue1%26name2%3Dvalue2" /> </object> |
Also note that the parameter names are case sensitive.
Notes
After stumbling around for a while, I discovered the following hint in the help for LoaderInfo.parameters.
An object that contains name-value pairs that represent the parameters provided to the loaded SWF file.
You can use a for-in loop to extract all the names and values from the parameters object. The two sources of parameters are: the query string in the URL of the main SWF file, and the value of the FlashVars HTML parameter (this affects only the main SWF file). |
Unfortunately, there are only two (2) occurrences of FlashVars in the ActionScript 3.0 sections of the help and no explanation of what it means.
It took searching the internet to get any real information. Once I found an example - search the page for FlashVars - the rest was fairly straight forward.
After creating this page, I discovered that the Flash ActionScript 2.0 help contains several examples. I never thought to look there because most of the AS 2.0 help just gets in the way ... which is why I typically keep it disabled. There are 2 sections
(The links go to the associated web pages which also contain user notes.) In the help, you will need to click (expand) the plus sign to see the examples ... on the web pages, the examples are already expanded. Notice that the way to access the parameters is different - the method I show above works for AS3, the help files show the AS2 method where variables with the correct names are automatically generated.
Also note that the AS2 help suggests using both FlashVars and LoadVars to get data ... but the AS3 help says that LoadVars is no longer used. This is why I leave the AS2 help turned off and don't bother testing its code examples.
Fails
<object ... > <param name="name1" value="value1" /> <param name="name2" value="value2" /> <embed src="myApplication.swf?name1=value1&name2=value2" /> </object> |
And
AC_FL_RunContent( ... 'name1', 'value1', 'name2', 'value2', ... ); //end AC code |
It was at this point that I searched the internet.
Debug Code
for (var s1:String in stage.loaderInfo.parameters) { trace(s1 + " = " + stage.root.loaderInfo.parameters[s1]); Filename_UIText.text = Filename_UIText.text + s1 + " = " + stage.root.loaderInfo.parameters[s1] + " : "; } |
I also comment out the <noscript> tag (by changing it to <!noscript> so that I can test both the JavaScript and the object code - this shows 2 copies of the same Flash movie.
ExternalInterface class
- How to call code in a web page, and
- How the web page can call code in the Flash movie
(See Example: Using the external API with a web page container)
The examples below require that ScriptAccess is enabled.
allowScriptAccess="sameDomain" |
The AS3 help shows where these go in the html file.
Calling Javascript
var Barcode:String = ExternalInterface.call("GetBarcode"); |
And the matching javascript code (in the related html file)
<script language="JavaScript"> <!-- // ------- functions called by ActionScript ------- function GetBarcode() { return "ABC-123"; } //--> </script> |
Called by JavaScript
import flash.external.ExternalInterface; if (ExternalInterface.available) { try { ExternalInterface.addCallback("Callback_AliasName", AliasFunction); } catch (error:SecurityError) { //output.appendText("A SecurityError occurred: " + error.message + "n"); } } else { // output.appendText("External interface is not available for this container."); } function AliasFunction(value:String){ ..... } |
In the web page (works in IE 6, fails in FireFox 1)
Multi-Browser
I found the following code as a user comment on the Adobe web site. I was able to verify that it works with with IE 6 and FireFox 1.
So this is what I am currently using
IE Failures
- The specific problem names are browser dependent, and
- There is no list of problem alias names
The following aliases are known to fail with Internet Explorer (IE) - play, stop, LoadMovie - note that Load_movie works.
When using LoadMovie, double clicking the Error on page warning (in the status bar) displays this totally unhelpful error.
Line: 24 Char: 3 Error: Type mismatch Code: 0 URL: http://....... |
When the name used in the JavaScript does not match the alias name, this error will be displayed.
Line: 48 Error: Object doesn't support this property or method |
Notice that the line number changed ... same code different line number ... very confusing.
play and stop generate
Error: Wrong number of arguments or invalid property assignment |
When testing these types of failures, beware of the various caches - your changes my not show up for several hours. Be sure to open a new IE window to load the most current swf file (Refresh - F5 - uses the cached version) - I use ctrl-n (New Window) with Internet Explorer.
Another failure occurs if you try to display the html file by simply double clicking it.
Adobe Flash Player has stopped a potentially unsafe operation. |
The message goes on to tell you how to change your security settings so this warning will not be displayed - don't do that, it will open your machine to attacks from other Flash applications. Since there is no failure when getting the page from the web, my suggestion is to open the html page using a local web server.
http://localhost/Generic_mcGraph/index.html |
Old Methods - fscommand
fscommand(command:String, args:String = ""):void Lets the SWF file communicate with either Flash Player or the program hosting Flash Player, such as a web browser. |
However, the AS3 (Flash 9) help says
If you are publishing for Flash Player 8 or later, the ExternalInterface class provides better functionality for communication between JavaScript and ActionScript ... |
Old Methods - JavaScript "SetVariable"
<script language = "JavaScript"><!-- function PassFlash(){ window.document.movie.SetVariable("text", "hello"); } //--></script> |
I have not tested this. It seems unlikely that this is useful. For one thing, it does not work with Macintosh. In addition, I don't know how the Flash application will know that there has been a change ... I guess the value could be checked with a timer.
Also, if you called a JavaScript function, it could set multiple Flash variables before returning.
Multiple Flash Applications
These applications can be in the same web page, or not. This technique can be used to develop popup windows to change parameters.
Kaynak Author: Robert Clemenzi - clemenzi@cpcug.org