Passing variables to Flex in the embed page (or URL string)

The Adobe Flex documentation sheds some light on passing variables to your Flex 2 application.  I tried the methods they describe but couldn’t get them to work because my Flex page uses the wrapper. I modified the html embed page that was produced by Flex in several ways to get my variable into Flex.

Parameter string:

 
<param name="flashvars" value='testvar=hello'>

On the embed tag (both passed to the SWF and using Flashvars:

 
<embed src="FlashVars.swf?testvar=hello" quality="high" bgcolor="#869ca7"
width="100%" height="100%" name="FlashVars" align="middle"
play="true"
loop="false"
quality="high"
flashvars='testvar=hello'
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.adobe.com/go/getflashplayer">
</embed>

These methods didn’t work of course because Flex creates the embed tags dynamically using JS files.    I placed it in the AC_FL_RunContent function:

 
AC_FL_RunContent(
"src", "FlashVars",
"width", "100%",
"height", "100%",
"align", "middle",
"id", "FlashVars",
"quality", "high",
"bgcolor", "#869ca7",
"name", "FlashVars",
"flashvars",'testvar=hello&historyUrl=history.htm%3F&lconid=' + lc_id + '',
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", http://www.adobe.com/go/getflashplayer
);

Inside of Flex, I created a small test function to read the value of the variable passed to the application:

 
<mx:Script>
<![CDATA[
     import mx.controls.TextArea;
     private var txt:TextArea;

     private function initApp():void
     {
         txt = new TextArea();
         txt.width = 500;
         txt.height = 500;
         this.addChild(txt);

         var testVar:String = Application.application.parameters.testvar;
         txt.text =   txt.text + ("testvar:" + testVar);
      }
]]>
</mx:Script>

Of course this post is not ground shattering, but it might save you a few minutes trying to hunt down and test the documentation files.

-Mr

28 Comments

  1. Hi,

    Is there a way to pass variable to a mxml document ?
    I mean when you use the flex data server and compile the mxml document from fds ?

    regards

  2. You know, I have not actually used FDS much, but its real similar to Flash Communication Server. I can imagine that you can pull or push data to your swf file if its compiled with the server, just like you would be able to with FCS. You may also want to post your question on the FlexCoders user group, they will be happy to fill you in on the answer to this question. http://tech.groups.yahoo.com/group/flexcoders/

  3. hopefully you are still watching this thread. I have a problem sending a parameter to a swf i’m loading as a module (with a custom made widget class, one that extends module loader).
    In order to load a module you have to specify it’s url; i’ve tried appending ?key=testValue to the url but when i trace Application.application.parameters.key i get undefined. Please let me know if you have done something similar, and how did you implement it. Please reply by e-mail also, if you can.

  4. Dude, I think you are best off using either an interface or using the ModuleManager. I have another post you should look at https://thanksmister.com/?p=56.

    We did try to pass in the var on the ModuleLoader url, but could never easily reference the var within the scope of the module. I think this is why Application.application.parameters.key did not work, the module is not the Application level, its loaded within a wrapper inside the Application and it uses the tag, not .

    Definitely try using an Interface or the ModuleManager method.

  5. Hi there:

    I apply the example on the top to pass a variable to flex modifying the AC_FL_RunContent.

    Thanks a lot That really rock!!!

    But i want to go furder so I have to ask you if you know a way to pass a dinamic variable generated by a function in JavaScript not a constant variable like in your example (cause flex always catch “HELLO” and no other strings generated by a javaScript)

    What I have to do to pass a random string for example. testvar = (“hello”or “foo” or “bar”)

  6. There was no real problem, this post just lists the basic methods for passing variables to a Flash file. Thanks for your input.

  7. dohh.. here’s the code (i had to remove the start and end tags from it)

    example call:
    loader.swf?imagen=sample.jpg

    code:

    mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
    xmlns=”*” paddingTop=”0″ width=”100%” height=”100%” styleName=”whiteStyle”
    layout=”vertical” creationComplete=”initApp(); ”

    ….

    mx:Script
    ![CDATA[

    [Bindable]
    public var im:String = ”;

    public function initApp():void
    {
    im:String = ‘images/’ + Application.application.parameters.imagen;
    }

    ]]>

    mx:VBox id =’imgBox’ width=”450″ height=”430″ paddingLeft=”24″ paddingTop=”10″ paddingBottom=”10″ backgroundImage='{im}’

    /mx:VBox

    /mx:Application

  8. ok, it works now..
    I just accidentally messed up the line in the initApp function, which should just be:
    im = ‘images/’ + Application.application.parameters.imagen;

    thanks anyways 🙂

  9. I would use a variable to set the styleName value for the VBox. In the CSS create a style that embeds the library reference to the SWF in your Flash library. But it looks like your method works for you.

  10. yeah, I originally used the styleName property for the VBox with a hardcoded style in my CSS file which embedded a fixed jpg. The issue I had however was that when I needed to change my app to allow different images (sent as a url parameter) to be the background , I couldn’t figure out a way to dynamically set that style for the VBox and thus came up with my way as a sort of work around.

  11. Hi I want to know if I can reference a txt file which contains the url that all my HttpServices will use for requests, Im trying to look for an answer but I could find anything related.

    Thanks

  12. Sure, just load the text file at the start and assign the variable to the url field of the httpservice. I use XML to keep all my external httpservice links so that I can switch servers easily with the applications.

  13. Wow– that was fantastic! Thank you. I also learned that the wrapper file gets overwritten everytime that Flex is changed. This can be VERY frustrating when testing. However, leaving the file open (DreamWeaver for me) allows me to quickly re-save my wrapper file.

  14. hi there, yes, it works and well, but, what if i want to pass some data when swf movie is already running?, does anybody know how to?, i wanna pass a finger print to a flex app but it’s already running when i got to use the finger print reader, besides i have not that info aviable when app starts. thanks

Comments are closed.