Intro to Flashvars | Passing variables to actionscript from the html embed | Tutorial

Google Buzz

I’ve had a couple special requests to explain flashvars and how to use it and show it in action.

Overview

The property “FlashVars” can be used to import root level variables to the flash movie or swf. The flashvars propery is used in codes for embedding flash in the html page. The string of variables passed in as flashvars, will be imported into the top level of the movie when it is first instantiated. Variables are created before the first frame of the SWF is played. The format of the string is a set of name=value combinations separated by ampersand (&) symbols.

Steps

  1. Include the flashvars property in your embed codes and voila! You have these variables to use in your swf.
  2. That’s the one step

Code

HTML Embed Codes

1
2
3
4
5
6
Here's some sample embed codes, including object and embed tags:
<object width="540" height="240" title="sample">
  <param name="movie" value="flashvarsTutorial.swf" />
  <param name="flashvars" value="var1=here&var2=are&var3=my&var4=flashvars" />
  <embed src="flashvarsTutorial.swf" flashvars="var1=here&var2=are&var3=my&var4=flashvars" type="application/x-shockwave-flash" width="540" height="240" ></embed>
</object>

Actionscript using flashvars

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//flashvars="var1=val1&var2=val2&var3=val3";

display("var1 = "+ var1);

display("var2 = "+ var2);

display("var3 = "+ var3);

display("var4 = "+ var4);

function display(todisplay:String){
  feedback.text += todisplay+"\n";
  trace(todisplay);
}

Example

Page 1 (var1=val1&var2=val2&var3=val3)
Page 2 (var1=here&var2=are&var3=my&var4=flashvars)

Source

Download the html files and the fla and swf in this flashvars.zip

  • del.icio.us
  • Digg
  • email
  • Facebook
  • FriendFeed
  • Google Bookmarks
  • Hexosearch
  • LinkedIn
  • Mixx
  • Print
  • PDF
  • StumbleUpon
  • Technorati
  • Twitter
  • RSS
This entry was posted in tutorial and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

15 Comments

  1. Posted 7 August 2008 at 7:39 am | Permalink

    Hey.

    Nice example for start..!

    You have managed your blog very well :)

  2. pedro
    Posted 22 August 2008 at 3:37 am | Permalink

    Hello, I have the files and everything and i was testing around with the fla file and the html page so it will only show up var 1 and 2 but even tho when i take them out of the fla and html page var 3 and var 4 they still show up and say undefined on it.. help?

  3. Jason Min
    Posted 24 November 2008 at 3:31 pm | Permalink

    If you use CS3 and publish html, you need to add
    In the AC_FL_RunContent code the following:

    ‘FlashVars’, ‘var1=val1&var2=val2&var3=val3′

  4. Posted 3 January 2009 at 8:25 pm | Permalink

    Thanks. It worked. I never knew it is so easy. I was actually messing a lot with the ActionScript 3.0 although I was only looking for something very simple like the example you provide. It was useful.

  5. Posted 7 March 2009 at 7:57 pm | Permalink

    Your example works while others I tried don’t. Thank you so much.

  6. Posted 27 March 2009 at 12:46 pm | Permalink

    Thank you very much. I found this example quite clear, compared to some others loose on the interweb.

  7. Steven
    Posted 28 May 2009 at 7:58 pm | Permalink

    I just downloaded the examples and tried running everything as is and I got “undefined” for all 4 variables. I tried with CS3 and CS4, any ideas on why that happened, is there some kind of setting that I need to enable? Please help…

    • Posted 30 May 2009 at 7:31 am | Permalink

      The flashvars will show undefined if you’re running from flash. They way to pass flashvars to the swf is in the embed code. So to actually test it you have to actually put it into an html file, good luck!

  8. G Dhurba
    Posted 8 September 2009 at 8:54 am | Permalink

    How do I load xml through flashvars?

  9. Posted 13 September 2009 at 3:06 pm | Permalink

    http://blog.circlecube.com/2008/04/tutorial/xml-and-flash-actionscript-made-easy-parse-xml-to-object-tutorial/ – here’s one example I’ve already written about. There are plenty of ways to do it and the best way for you may depend on a few factors (for starters, as2 or as3?)

  10. Posted 5 January 2010 at 9:38 am | Permalink

    Nice clean tutorial and the examples work for me, but I wonder why as I can’t use the same approach in my class file.

    Why doesn’t the AS3 compiler complain that these variables are undefined?

  11. Posted 5 January 2010 at 4:03 pm | Permalink

    @Tristan – notice that this is for as2. I have another tut for as3 and flashvars… http://blog.circlecube.com/2009/12/tutorial/flashvars-and-as3/

  12. Posted 14 February 2010 at 2:44 pm | Permalink

    Hello,
    The tut works great if just testing as is, but when I add the AC_FL_RunContent() script, with
    ‘flashvars’, ‘var1=here&var2=are&var3=my&var4=flashvars’,

    added in the AC code, it does not display. I’ve been searching for hours to come up with a solution that will work for FlashVars.

    Thanks much,
    Greg

  13. Posted 14 February 2010 at 8:10 pm | Permalink

    Have you looked at SWFObject? It works great and gives you a variety of options for displaying your SWFs.

  14. Posted 16 February 2010 at 11:07 am | Permalink

    @Greg – yes I love swfobject, it’s the only tool I use anymore for embedding flash content online. CS5 will actually use swfobject rather than AC_Active whatever trash it uses currently.

Post a Comment

Your email is never published nor shared. 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>