Passing Parameters To SWF File with Actionscript 3.0

Recently I had a project to build a Flv player with flash. The video file name and the title were to be passed to the flash file as parameters from the html file. Since I had not done passign parameters to flash file with AS 3.0 I search the net to see how its done. There were many tutorials and the solution seemed very simple and straight forward. So I left this part to the very end of the project. But when I tookup this part it didn’t work as expected. Finally I figured it out and thought that I will share it with all of you.

How to pass parameters to flash (swf) file

First we will begin with the passing the variable to the SWF file from the html file. This was where I went wrong. I tried the following ways that didn’t work for me.

<param value="flashfile.swf?par1=hello&par2=world" .....

<param value="flashfile.swf" flashvars="par1=hello&par2=world" .....

<embed src="flashfile.swf?par1=hello&par2=world" .......

<embed src="flashfile.swf" flashvars="par1=hello&par2=world" .....

finally what worked was including the parameter in the javascript as shown below.

AC_FL_RunContent(
'codebase', 'http://......',
'width', '500',
'height', '400',
'src', 'flashfile',
 'flashvars', 'par1=hello&par2=world',
...........
)

or

AC_FL_RunContent(
'codebase', 'http://......',
'width', '500',
'height', '400',
 'src', 'flashfile?par1=hello&par2=world',
...........
)

Publish your flash file and edit the html file generated to add the ‘flashvars’.

Accessing flash variables with actionscript 3.0

Now we need to access these variables from flash file with actionscript. The flash variables are included as properties of  ‘root.loaderInfo.parameters’. So to access our parameters we simply have to write,

trace(root.loaderInfo.parameters.par1);

trace(root.loaderInfo.parameters.par2);

NB:
If the SWF file is called directly by the browser the parameters can be passed in the traditional way that they are passed to the script files (url embeded)
eg : http://yourdomain.com/flashfile.swf?par1=hello&par2=world

This entry was posted in Web Development and tagged . Bookmark the permalink.

6 Responses to Passing Parameters To SWF File with Actionscript 3.0

  1. tim says:

    awesome! u rock…

  2. Sharedtut says:

    Great job, thank you for sharing.

  3. Chi says:

    Thanks so much for your help, i’ve already spent hours on this!

  4. Rana says:

    This is exactly what I am searching. Thanks a lot for sharing this.

    Regards

  5. Jorge Klemm says:

    Hi Alfie,

    Your Blog is clean and objetive..

    I’ve search this informations spend a lot of time, and it’s here.. Great !!

    Other way to get parameters…

    function getSwfParam(name:String, defaultValue:String):String {
    var paramObj:Object=LoaderInfo(stage.loaderInfo).parameters;
    if (paramObj[name]!=null && paramObj[name]!=”") {
    return paramObj[name];
    } else {
    return defaultValue;
    }
    }

    Call the function above with the NAME of the param and the default value that the function will return in case not found param called NAME. So simple yeah ?

    Thank You.

  6. Hi Alfie,

    good to the point post on ways of adding parameters to flash swfs. Thanks!

    BTW did you know that if you don’t have prior knowledge about which parameters will be passed to the swf that you can iterate over all of them like this

    var keyStr:String;
    var valueStr:String;
    var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
    for (keyStr in paramObj) {
    valueStr = String(paramObj[keyStr]);
    //you now have a key and a value do something with this information
    }

    and in Flex 3 this works the same but you can substitute line “var paramObj:Object…..” with:

    var paramObj:Object = Application.application.parameters;

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>