EladElrom.com

Deep Dive Into Technology

Passing variables through URL using flashvars Flex 4

Using Flash builder 4.6 HTML page generator it took me 5 mins to figure out how to pass variable since Adobe’s page (http://kb2.adobe.com/cps/164/tn_16417.html) is not updated to show how it’s being done using Flex 4 and on the latest index.template.html page.

To add the variables you would need to add the variables as describe here:
http://code.google.com/p/swfobject/wiki/documentation

var flashvars = {
  name1: "hello",
  name2: "world",
  name3: "foobar"
};
var params = {
  menu: "false"
};
var attributes = {
  id: "myDynamicContent",
  name: "myDynamicContent"
};

In your Flex application you will be able to read there easily:

var params:Object = FlexGlobals.topLevelApplication.parameters;

In case you want to use Javascript and pass the variable through the URL you can add the following script to the HTML document:

        <script type="text/javascript">

			function getHeaderUrlVariables() {
				var vars = {};
				var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
					vars[key] = value;
				});
				return vars;
			}

			var sessionId = getHeaderUrlVariables()["var1"];
			// alert(sessionId);

			var flashvars = new Object();
			flashvars.var1 = var1;

           </script>

Remember to remove the other flashvars tag: var flashvars = {}

And once you pass the variable through the URL you will be able to read it:

var params:Object = FlexGlobals.topLevelApplication.parameters;
Alert.show(params.var1);

Category: Flex, Tips & Tricks

Your email address will not be published. Required fields are marked *

*