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'), ...