View comments | RSS feed

Using FlashVars in an application

Using FlashVars to pass variables into Flash is similar to passing variables along the URL in the HTML code. With FlashVars, instead of passing variables after the filename, variables are passed in a separate param tag as well as in the embed tag.

To use FlashVars in a document:

  1. Create a new Flash document, and name it myflashvars.fla.
  2. Select File > Publish Settings and make sure that HTML is selected, and then click OK to close the dialog box.
  3. Add the following ActionScript to Frame 1 of the main Timeline:
    this.createTextField("myTxt", 100, 0, 0, 100, 20);
    myTxt.autoSize = "left";
    if (_level0.myURL == undefined) {
        myTxt.text = "myURL is not defined";
    } else {
        myTxt.text = _level0.myURL;
    }
    

    NOTE

     

    By default, HTML code publishes to the same location as myflashvars.fla.

  4. Select File > Publish to publish the SWF and HTML files.
  5. Open the directory containing the published files (where you saved myflashvars.fla on your hard drive) and open the HTML document (myflashvars.html by default) in an HTML editor such as Dreamweaver or Notepad.
  6. Add the code that appears in bold below, so your HTML document matches the following:
    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="550" height="400" id="myflashvars" align="middle">
    <param name="allowScriptAccess" value="sameDomain" />
    <param name="movie" value="myflashvars.swf" />
    <param name="FlashVars" value="myURL=http://weblogs.adobe.com/">
    <param name="quality" value="high" />
    <param name="bgcolor" value="#ffffff" />
    <embed src="myflashvars.swf" FlashVars="myURL=http://weblogs.adobe.com/" quality="high" bgcolor="#ffffff" width="550" height="400" name="myflashvars" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
    </object>
    

    This code passes a single variable called myURL, which contains the string
    http://weblogs.macromedia.com. When the SWF file loads, a property named myURL is created in the _level0 scope. One of the advantages of using FlashVars or passing variables along the URL is that the variables are immediately available in Flash when the SWF file loads. This means you don't have to write any functions to check if the variables have finished loading, which you would need to do if you loaded variables using LoadVars or XML.

  7. Save your changes to the HTML document, and then close it.
  8. Double click myflashvars.html to test the application.

    The text http://weblogs.macromedia.com, a variable in the HTML file, appears in the SWF file.

    NOTE

     

    All browsers will support string sizes as large as 64K (65,535 bytes) in length. FlashVars must be assigned in both the object and embed tags in order to work on all browsers.


Flash CS3


Comments


animagics said on Jul 23, 2007 at 9:06 AM :
This DOESN'T WORK unless you add FlashVars to the Javascript part of the HTML as well.

Find the "AC_FL_RunContent" function and add the following line to the middle of the list of parameters:
'FlashVars', 'myURL=http://weblogs.adobe.com/',
da4seen said on Jul 23, 2007 at 6:53 PM :
It would be nice if the example was more detailed:

this.createTextField("myTxt", 100, 0, 0, 100, 20);
myTxt.autoSize = "left";
if (_level0.myURL == undefined) {
myTxt.text = "myURL is not defined";
} else {
myTxt.text = _level0.myURL;
}
----

1. What does the numbers mean in:
- this.createTextField("myTxt", 100, 0, 0, 100, 20);

2. How exactly does the ".createTextField" work and why is it needed in this example?

3. What does "_level0." mean?

Preferably it would be good if the tutorial went into further detail beginning with creating the text box in flash and explaining exactly what else is needed.
No screen name said on Aug 6, 2007 at 11:10 AM :
Searching for hours and a simple mistake in the example because it is no
up to date for cs3...
No screen name said on Aug 7, 2007 at 3:25 PM :
1) this.createTextField("test_txt", 10, 0, 0, 300, 100);
This code creates a 300 x 100-pixel text field named test_txt with a location of (0, 0) and a depth (z-order) of 10.

2) It is used to create a place for the message to display. It is easier for MM (or Adobe) to "create" a textfild via text/code than it is by screenshot and frame.

Essentially same thing as when you press the text button in the GUI

3) Root
todddominey said on Aug 7, 2007 at 4:40 PM :
Yeah, Adobe really needs to update this document with the ActiveContent
javascript info noted in comment #1, especially since that's the default way
that Flash publishes now.
AmarShah said on Sep 10, 2007 at 10:28 AM :
Hi,
I try to follow the same steps described in the Note but it doesn't work I also add the FlashVars in the JS but still doesn't work. Any one know what could be the issue??
kaos2 said on Sep 10, 2007 at 11:23 AM :
I went searching for a solution for AS3 and found what I was looking for:
To access the flashvars passed in use this (in AS3):

root.loaderInfo.parameters.MyVariableName
go mama go said on Sep 21, 2007 at 11:35 AM :
Kaos2, can you post your solution and your code? I'm also using CS3
and get errors when I follow this. thanks so much!
AndreLelis said on Nov 22, 2007 at 10:50 AM :
In IE6 the FlashVars doesn´t work,
To fix it you have to pass the variables in the movie address like this

<param name="movie" id="mymovie.swf?var1=foo&var2=bar" />
TessandraFae said on Mar 4, 2008 at 7:55 AM :
Ok everybody,
I figured it out.

Flash Player 9, update 3 has changed the rules a bit. You now have to declare AllowAccess in order for flash buttons to change the html navigation. See this article for more details on the change:

http://kb.adobe.com/selfservice/viewContent.do?externalId=50c1cf38&sliceId=2

Now, to make a generic flash button, that you can set an html flash var in the html page, do the following:

In the AC_FL_RunContent code, add the following:
'allowScriptAccess','always',
'FlashVars', 'myURL=mywebpage.html',

In the params, add the following:
<param name="allowScriptAccess" value="always">
<param name="FlashVars" value="myURL=mywebpage.html">

In the embed, add the following:
allowScriptAccess="always"
FlashVars="myURL=mywebpage.html"

Inside the Flash button, at the top most layer, Frame 1, add this code:
mybtn.onRelease = function(){
getURL(_level0.myURL,"_self");
}

Make sure your button symbol is given the instance name "mybtn"

Publish your swf and your html pages. You're buttons should now function properly.

Cheers!
Tessy
No screen name said on Jul 29, 2008 at 12:34 PM :
Be sure that your movie works properly on any browser. I tried the tip above but didn't work in Camino. (I bet it doesn't work in Safari neither)


This solved my problem.

AC_FL_RunContent(
'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
'width', '395',
'height', '178',
'src', 'fotos_oferta',
'quality', 'high',
'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
'align', 'middle',
'play', 'true',
'loop', 'true',
'scale', 'noscale',
'wmode', 'window',
'devicefont', 'false',
'id', 'fotos_oferta',
'bgcolor', '#ffffff',
'name', 'fotos_oferta',
'menu', 'true',
'allowFullScreen', 'false',
'allowScriptAccess','sameDomain',
'movie', 'fotos_oferta',
'salign', '',
'FlashVars','param_note=erase_the_arrow_on_the_right' <-------
); //end AC code

Regards
Quique

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000668.html