Jun
23
2009

Passing values from website URL (GET method) to Flash

There are plenty of situations when you may need to pass arguments from URL to the Flash. In my case I was looking for a way to change the Flash banner on a corporate website to display different content for different marketing campaigns. Company runs number of different campaigns for different products, publishing many ads on IT portals. The Flash banner on company's website will display a different advert according to the parameter in URL that will be passed from these ads. (like http://company.com/index.php?campaign=5).

I decided that Flash will be embedded in the PHP file. First we need to use PHP to get the GET variable from the URL. We can achieve that reading $_GET['variable_name']. Next, we need to write this to the FlashVars in <embed> and <object>. Then the last step is to retrieve this variable from ActionScript using loaderInfo.parameters.variable_name.

There are three places to add a variable in the HTML generated from Flash (and renamed to PHP) when publish from Flash to HTML. First search for the JavaScript:

1
2
3
<script language="JavaScript" type="text/javascript">
AC_FL_RunContent(
'FlashVars', '<?php echo "name=".$_GET['mymessage'];?>',

 

Next add a tag between the <noscript> tags:

1
2
3
4
5
noscript>
<object classid="" codebase="http://download.macromedia.com/pub/
shockwave/cabs
/flash/swflash.cab#version=8,0,0,0"
width
="800" height="600" id="demo_ap_IRE" align="middle">

<param name="allowScriptAccess" value="sameDomain" />
<param name="FlashVars" value="<?php echo "name=".$_GET['mymessage'];?>" />

 

And in the same object, to the <embed> tag.

1
<embed src="banner.swf"  FlashVars="<?php echo "name=".$_GET['mymessage'];?>" 

 

Below is an ActionScript code to get FlashVars. There is a dynamic text field created on the stage called myDisplay.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.LoaderInfo;

public class FlashVarsExample extends MovieClip
{
var flashVars:Object;
public function FlashVarsExample()
{
this.loaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
}

function loaderComplete(e:Event):void
{
flashVars = this.loaderInfo.parameters;
if (flashVars.mymessage != null)
myDisplay_txt.text = flashVars.mymessage;
else
myDisplay_txt.text = "error";
}
}

 

Check below the live example, change mymessage URL parameter in your address bar and check the effect:
Link

 

AS3 Tips

I'm with Adobe - Facebook group