Select Page

FlashVars AS3 – getting values from the URL string

Getting values from the URL string in AS3 isn’t very complicated. First you need to set up your html to pass these into the SWF using a javascript function:


Place this inside your html.
Then if you are using the default HTML publishing you need to set up your SWF to take the variable. In this example I’m looking for a variable called ‘transition’



...




...

The important lines are:

‘FlashVars’, ‘transition=’+gup(“transition”),
and
param name=”FlashVars” value=”transition=”+gup(“transition”)

Here we are calling the javascript function to return the value that matches ‘transition’. Then this gets passed to the SWF.

Inside the flash this can be accessed with:

var sTrans:String = String(stage.loaderInfo.parameters.transition)

Then you can get variables from the URL string:

http://www.sitedaniel.com/index.html?transition=myvalue

Getting FlashVars from the URL – javascript function

Here’s a very handy javascript function to grab FlashVars from the URL. ‘GUP’ Get URL Parameters can sit in your html or in an external js file.

function gup(name){
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null ) return "";
    else return results[1];
}

And then when you create your SWF object, pass in the value name you are looking for (in this case ‘page’)

var so = new SWFObject("loader.swf?page="+gup('page'), ...

Opening Pop-up windows from Flash

Recently I read that the best way to open pop-up windows from Flash was to use the ExternalInterface class. I found some code that looked good but it didn’t work in Safari on a Mac.

if (ExternalInterface.available) {
	ExternalInterface.call("window.open",
				"popup.html",
				"win",
				"height=300,
				width=400,
				toolbar=no,
				scrollbars=yes");
} else {
	getURL('popup.html', '_blank');
}

However it isn’t that hard to just use getURL and call a javascript function set in the html page:

//javascript
	function openPopup() {
		winWidth = 450;
		winHeight = 680;
		screenWidth = screen.availWidth;
		screenHeight = screen.availHeight;
		if (screenWidth < winWidth) {
			winWidth = screenWidth;
		}
		if (screenHeight < winHeight) {
			winHeight = screenHeight;
		}
		winTop = (screenHeight - winHeight) / 2;
		winLeft = (screenWidth - winWidth) / 2;
		window.open(\'page.html\',\'Title\',\'width=450,
				height=680,scrollbars=no,
				status=no,resizeable=yes\'+ \',
				left=\' + winLeft + \',
				top=\' + winTop);
		//window.focus();
	}

So far this works in all browsers.

URL encoding in Javscript

After almost freaking out about a problem with FlashVars and a URL string variable that contained many ampersands (&) I couldn’t work out how to pass the URL in without Flash splitting the string up into separate variables until I came across this lovely JS function

encodeURIComponent(…)

This is a good one to remember