!!!!UPDATE!!!!!!
I’ve wrote a quick cookbook recipe on how to handle flashvars, published on Adobe’s Cookbook
Here the URL to the recipe: Reusable flashvars class http://bit.ly/d3eUpl
————————————————————————-
Everybody knows that FlashVars are pretty useful when you want to work with dynamic data being passed from the URL or from the object tags, that gives you a bit of a dynamic touch to your application as well allows you to shorten out your delivery time since you can create your flash object only once, for example you want to publish 100′s of videos like youtube does for example, but you don’t want to create for every clip a single swf.. Believe me there are people out there that are still doing that. If you don’t have a big ass database like most web 2.0 sites have, then flash vars is the intermediate solution.
Actionscript 2.0:
While we’re still scripting AS 2.0 in flash 8, we could just define an empty variable in our first frame, and the values being passed from the URL or through the Embed object were passed to our flash movie.
In the example below I’ve created a swf that it’s getting the content of it’s textfield through FlashVars using Actionscript 2.0
Example:
var myFlashVar:String; _root.createTextField("myTextField", 1, Stage.width/2 - 50, Stage.height/2 , 150, 40); myTextField.text = myFlashVar;
…
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100" height="100" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="flashVar_AS2.swf" /><param name="bgcolor" value="#ffffff" /><param name="quality" value="high" /><embed type="application/x-shockwave-flash" width="100" height="100" src="flashVar_AS2.swf" quality="high" bgcolor="#ffffff"></embed></object>
…
and here:
flashVars="myFlashVar=THIS IS MY TEXT"
You just need to add the flashVars parameter to your object & Embed tag and you are ready to go, don’t forget to create a new variable in you AS project otherwise there might be errors during FlashVars are being processed.
Actionscript 3.0
With actionscript 3 a few things changed, it’s not enough just creating a variable, you need to tell flash to go pickup the flashvars otherwise nothing will happen, how you do that? Read on…
//FLASHVARS CODE var varName:String; var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters; for (varName in paramObj) { myFlashVar = String(paramObj[varName]); } //INIT VARIABLES var myFlashVar:String; var myTextField:TextField; //TEXTFIELD PROPERTIES myTextField = new TextField(); myTextField.text = myFlashVar; myTextField.x = stage.width / 2; myTextField.y = stage.height / 2; addChild(myTextField);
As on the previous example we create a new textfield assign the content of it to our FlashVars variable and add it to the stage with the addChild method.
The FlashVars code (commented) is the only piece you need to do to be able to read out the values of it.
First of all we define varName which will be the showing us the name of the variable, paramObj is going to be our variable content
During the for loop we pass our flashVars to the appropriate variable, if you were passing multiple variables you would just need to rewrite the for loop and assign the flashvar to each variable.
For the HTML you just need to add the exact same code as we did before for our AS 2.0 example.
I hope that people understand how useful Flashvars really are for your daily work, just keep something in mind which is very important, don’t pass sensitive data using this method as everybody can see what you passing by doing a right click and view source.
Tiago




http://video2.channelnewsasia.com/cnavideos/chineseplayer.asp?skin=Player1.swf&player=chineseplayer.swf&filename=xin_042308_fannwong2.flv
hi, is there a way to download the above flash video?
cos whenever i tried to download its just the player skin and not the video.
or what other information do i need to be able to download?
Abobe code AS3 – is not working, will u explain in detail about this.
Finally! A FlashVars tutorial that’s easy to follow and that ACTUALLY WORKS straight from this page.
Thanks a lot.
Hi there, thanks for the great tuturial. How would I apply this to streaming video playlist? I have it set up dynamically and can include any necessary parameters in the XML for each vid but I don’t know how to tie it all together. Any help you can give I would really appreciate. Thanks!
Hello, im Lucas from Argentina. Sorry for my poor english!!
I have a problem with FlashVars. In this example you put the text in the parameter (“myFlashVar=THIS IS MY TEXT”). How can I do if I want to get the text of the parameters from a textfield in the html page instead of write it with the keyboard?
I mean to make something like:
param name=”FlashVars” value=”myFlashVar=” + txtPar.value
I have tried this, but i get an error. Can you help me?
Thanks a lot! And sorry for the mistakes in the text!
Thanks a lot.
1. Please visit http://www.busycode.com or http://www.flexdeveloper.com.cn
2. Low cost high quality.
3. Best Adobe Flex outsourcing service provider.
4. More than 60 full time in-house Flex developers.
5. Our company Busycode Inc. was registered in San Francisco, Beijing and Nanning.
6. Our skill set is “Adobe Flex/AIR/Flash + .NET/Java/PHP + SQL Database”.
7. Our keywords are Flex developer, Flex coder, Flex programmer, Flex expert, Flex engineer, Flex specialist and Flex outsourcing service provider.
Thank you for this tutorial. It is helpful, I have one question though:
when my text is displayed in the .swf it has a font face of time new roman. How can I change this font, to say arial of veranda?
To change the font to one like arial, you use the textFormat object and set the properties on it.
For example
var tf:TextFormat = new TextFormat();
tf.font = "Arial";
textbox.setTextFormat(tf);
textbox.embedFonts(true);
it is great to see the toturial.Before i was much worry about then flashvars .now it’s seem easy to me.
I hate using tutorials that don’t seem to work straight from the code but some people say it does
I’m wrong, it works. I’m eating crow
My last post no matter what. It does and it doesn’t work. When I put this code in I can get text to display in the HTML but in Flash I get:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
at promotions_fla::MainTimeline/promotions_fla::frame1()
Few more ways to pass variable to flex
http://yasob.blogspot.com/2009/05/accessing-http-request-parameter-in.html
“don’t pass sensitive data using this method as everybody can see what you passing by doing a right click and view source”
so what do i do if i need to hide the data?
Example did not work for me, same error as Matt:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
at promotions_fla::MainTimeline/promotions_fla::frame1()
@Matt
you need to give your text field an instance name
@michelle
wow u are an idiot
im getting the same error as Gordon and Matt, it says must be non-null.
what does this mean???
i HAVE set an instance name for my text field, and then I inserted that into the code in place of “myTextField”
can anyone paste the code the way they did it with their instance names? or describe how this is donw??
u r dumb
Dude, you overcomplicated something that’s as simple as:
//AS3
var paramObj:Object = this.root.loaderInfo.parameters;
var myFlashVar:String = parameters.myFlashVar;
//or this (still AS3)
var myFlashVar:String = this.root.loaderInfo.parameters.myFlashVars;
Dude, you overcomplicated something that’s as simple as:
//AS3
var paramObj:Object = this.root.loaderInfo.parameters;
var myFlashVar:String = paramObj.myFlashVar;
//or this (still AS3)
var myFlashVar:String = this.root.loaderInfo.parameters.myFlashVars;
if u dont know how to do it also dumb la, dont say people dumb
thx very much
i’ve met a lot of problem in as3 since read this
Thank you Clay, for pointing it out so simple. Works like a charm!
Brother! You saved my life!
It’s the better exemple that I found and all World Wide Web!
Thanks a lot!
Why the hell are you init a var AFTER YOU USE IT??
//INIT VARIABLES
var myFlashVar:String;
Is AS3 really this shit?
And it still doesn’t work. Damn I hate flash with a passion.
cal, the variables are being created not instantiated!! the init comment is meant as “initial”
works perfect..
@Clay, there are many ways of accomplishing this, I think this way is good for beginners so they understand how it works.
Hi.here I used another code for a html document named ‘Test.html’ but unfortunately it didnt work:
import flash.text.TextField;
var Url:URLRequest = new URLRequest(“Test.html”);
var vars:HTMLLoader = new HTMLLoader();
vars.load(Url);
vars.addEventListener(Event.COMPLETE,onComplete);
function onComplete(evt:Event):void {
var tf:TextField = new TextField();
tf.x = 20;
tf.y = 20;
tf.width = 200;
tf.height = 200;
tf.border = true;
addChild(tf);
var key:String
var params:Object = loaderInfo.parameters;
for (key in params) {
tf.appendText(key + “: ” + params[key] + “\n”);
}
}
Could you please help me how to fix it?
best regards
Alis
Test.html:
param name=”flashvars” value=”foo=bar&name=peter&site=actionscriptexamples.com” /
@Alis why exactly do you want to load an HTML file into flash?? Is there a special reason for that?
You can easily load the variables from the HTML that hosts the SWF file.
Actually I created a mp3player.swf file for a site and the track number is generated at html so my mp3player should read the html content source{for example parameter name}and play the song.
Best regards
Alis
@Alis – is the mp3player embedded in the test.html file? If yes then you don’t need to load anything with the HTMLLoader just use the stage.loaderInfo example above and you should get the parameters, have a look at the update I posted.
What I would do is this:
1. Create the mp3player swf and embed in the html file, there you put flashvars with the track information you need.
2. get flashvars with the method mentioned above and everything should work perfect..
If you can’t make it, shoot me the .fla file containing your player and I will take a look at it.
I publish my mp3player with html using flashCS4 publish setting but the source html file is so complex even to understand{318 lines of code}.I’m not a HTML expert coder but my employer told me to set mp3player communicate with HTML in order to handle different users connecting at the same time to Website.
The test.html was just a test for me to understand how the process to read the HTML source to apply into my mp3player AS3 and as u see i couldn’t.
The track information is generated by another program at Embedded Object of HTML file So I want to force my embedded SWF file to read and assign it to my track var script.
How to shoot u the fla file?
It is your kind to solve my problem.
Thank u so much.
Alis
Awesome post, Your blog is really nice. I’ve found lots of helpful info. Thanks!
Hi there,
I am trying to simply pass some information from the HTML to an existing text field in Flash. Soon it will be to pass a username, but for now it’s just testing. For some reason it isn’t working.
Here is what I have in Flash:
function loaderComplete(myEvent:Event)
{
var flashVars=this.loaderInfo.parameters;
user_txt.text=flashVars.username;
}
this.loaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
And my HTML has params set up like this:
Any help on this would be much appreciated!!!
Thank you,
Redjester
Oops, here is what I have to call if from the HTML:
FlashVars=”username=Test%20Name” width=”550″ height=”400″