3 Replies Latest reply on Aug 19, 2007 5:35 PM by Henk2

    Pass parameters using SFWLoader

    Henk2
      I need to load a (AS2/AS3) SWF from within a AS3 SWF and pass parameters to the child. This works great using FlashVars when loading a SWF from JavaScript. Now I need to do the same from with a Flex2 SWF.

      I've read several posts where people are successful in passing parameters from a flex 2 app to another SWF, using SWFLoader. I've spend a day trying to get this to work but no luck. Read lots of forum posts and some people claim that it works for them.

      The following code is an example. A swf that loads itself again while showing the parameters in a label. If you run this in a browser with "test.swf?var1=hello-world" then it nicely shows hello-world in the label. Hit the load button and it is supposed to load the same swf with another var1 parameter. The SWF loads fine but tThis time the label is empty. The Query part of the URL that contains the parameters seems to be ignored. What am I doing wrong here?

      quote:


      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application
      xmlns:mx=' http://www.adobe.com/2006/mxml'
      verticalAlign="top"
      width="100%"
      height="100%"
      creationComplete="init()"
      >
      <mx:Label id="appParmsLabel" text="URI Parameters:"/>
      <mx:Button label="Load test.swf?var1=123" click="load1();"/>

      <mx:Script><![CDATA[
      public var SWFURL:String = "test.swf?var1=123";
      private function init():void {
      application.appParmsLabel.text += getAppParms();
      }
      // Get the application parameters
      private function getAppParms():String {
      var result:String = "";
      for ( var name:String in application.parameters )
      {
      result += name + ":" + application.parameters[name] + ", ";
      }
      return result;
      }

      // load flash swf
      private function load1():void {
      child.source=SWFURL;
      }
      ]]></mx:Script>

      <mx:SWFLoader id="child" />
      </mx:Application>

        • 1. Re: Pass parameters using SFWLoader
          Henk2 Level 1
          bump
          • 2. Re: Pass parameters using SFWLoader
            ntsiii Level 3
            I guess I have to be one of those for whom this just works. I do not see anything wrong with the code.

            What does the loaded swf look like? You need to be working with an AS3 swf to start with.

            Tracy
            • 3. Re: Pass parameters using SFWLoader
              Henk2 Level 1
              Thanks for the reply,

              It is good to know that this actually does work for someone.
              Maybe it needs some libraries linked in?

              I use the flex commandline compiler to build this example, like this:
              mxmlc -output test.swf -compiler.source-path=src/ src/MainApp.mxml

              Then run the resulting test.swf file in the browser.
              test.swf?param1=hello

              The window opens and shows "URI Parameters:param1:hello" with the 'Show" button on the next line.
              Then clicking the Show button, it loads the same test.swf again using SWFLoader. The code uses test.swf?var1=123 as the URL. The swf loads fine, but displays "URI Parameters:".

              So in this case an AS3 SWF loads the same AS3 SWF. FlashVars still don't work.

              At first I tried loading an AS2 SWF. The SWF loaded and ran fine but FlashVars weren't passed in. If both are AS3 SWF's then a workaround is to set the application parameters directly. But I need this to work from AS3 loading AS2 SWF's.

              So FlashVars are passed when loaded through the browser but not when loaded through SWFLoader.

              # mxmlc --version: Version 2.0.1 build 155542