Sunday, 15 April 2012

Passing FlashVars to SWF from HTML

In this quick post I will show you how to pass FlashVars to the SWF file from the HTML in Actionscript 3. I will be using the SWFObject to pass the FlashVars instead of using the Object tag in HTML.

I have created a text field using the TextField class and passed the variable ‘message’ from the FlashVar.

var flashVars:Object = LoaderInfo(this.root.loaderInfo).parameters;
   
var tf:TextField = new TextField();
tf.text = flashVars.message;
addChild(tf);

The HTML code will look like this.

<!DOCTYPE HTML>
<head>
 <title>FlashVars example</title>
<script src="swfobject.js"></script>
<script>

var flashvars = {
 message: "hello ilike2flash"
};
var params = {};
var attributes = {};

swfobject.embedSWF("myContent.swf", " flashContent ", "550", "400", "10","expressInstall.swf", flashvars, params, attributes);

</script>
 
</head> 
<body> 
 <div id="flashContent"></div>
</body> 
</html>

Note this process is the same for passing a clickTag variable if you are familiar with building Flash banner ads. If you are testing in the Flash IDE your text field will return null because the FlashVar is setup in the HTML so the value cannot be accessed. To solve this problem you can test your swf in a web browser, or alternatively you can set a default value for when you are testing in the Flash IDE like this.

var tf:TextField = new TextField();
tf.text = flashVars. message || “hello ilike2flash”;
addChild(tf)

The same works if you have multiple FlashVars in the HMTL. I have three text fields beneath each other. Each text field is passing a different FlashVar.

var flashVars:Object = LoaderInfo(this.root.loaderInfo).parameters;
   
var tf1:TextField = new TextField();
tf.text = flashVars.message1;
addChild(tf1);

var tf2:TextField = new TextField();
tf2.y = 100;
tf.text = flashVars.message2;
addChild(tf2);

var tf3:TextField = new TextField();
tf3.y = 100;
tf3.text = flashVars.message3;
addChild(tf3);

The HTML code looks like this. I have only included the script tag and not the rest of the HTML.

<script>

var flashvars = {
  message1: "hello ilike2flash",
  message2: "Bonjour ilike2flash",
  message3: "Guten tag ilike2flash",
};
var params = {};
var attributes = {};

swfobject.embedSWF("myContent.swf", " flashContent ", "550", "400", "10","expressInstall.swf", flashvars, params, attributes);

</script>

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP