Passing URL parameters to a Flash application

I have identified three cases in which you may want to extract parameters passed via the URL to your Flash application:

a)The URL is something like this: http://www.test.com/page.html?parameter=test. The SWF file is included in the HTML page.

Solution: Use the ExternalInterface to extract the data from URL. It’s not a nice solution but I do not know another option. The code is below:

private function readParameters():Object{
 var params:String =
  ExternalInterface.call(“window.location.search.substring”);
 return (params?(new URLVariables(params.substring(1))):null);
}

b)The URL is something like this: http://www.test.com/page.jsp?parameter=test. The SWF is included in the JSP page. The JSP will be rendered to produce HTML output.

The same solution from a) works. However, it is also possible to read the parameters from the Java code with request.getParameter() and add them to AC_FL_RunContent.. invocation.

    AC_FL_RunContent(
            “src”, “test_test”,
            “width”, “100%”,
            “height”, “100%”,
            “align”, “middle”,
            “id”, “test_test”,
            “quality”, “high”,
            “bgcolor”, “#869ca7″,
            “name”, “test_test”,
            flashvars, paramOne=test1&param2=test2,
            “allowScriptAccess”,“sameDomain”,
            “type”, “application/x-shockwave-flash”,
            “pluginspage”, “http://www.adobe.com/go/getflashplayer”
    );

Now you can read the parameters with the following code:

var params:Object = Application.application.parameters

c)The SWF is called directly. for example http://www.test.com/myswf.swf?parameter=test

In this case you can read the parameters again using the code from b)

var params:Object = Application.application.parameters

4 Responses to “Passing URL parameters to a Flash application”

  1. Sebastian Says:

    Hi,

    you can also use swfObject. It provides a nice way to send Variables to a Flash app, be it URL-Variables or hardcoded values inside the html.
    you can find this here:
    http://code.google.com/p/swfobject/

  2. cornel Says:

    Thanks for the info

    Cornel.

  3. Andrew Odri Says:

    The method I tend to use is the following:

    1. Use the following function to extract the query variables:

    function getQueryParameter(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split(”&”);
    for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split(”=”);
    if (pair[0] == variable) {
    return pair[1];
    }
    }
    }

    2. Embed the *.swf using SWFObject, then call getQueryParameter(variable) and assign it the appropriate flash variable:

    var flashvars = {
    name1: getQueryParameter(”name1″),
    name2: getQueryParameter(”name2″),
    name3: getQueryParameter(”name3″)
    };
    var params = {
    menu: “false”
    };
    var attributes = {
    id: “myDynamicContent”,
    name: “myDynamicContent”
    };

    swfobject.embedSWF(”myContent.swf”, “myContent”, “300″, “120″, “9.0.0″,”expressInstall.swf”, flashvars, params, attributes);

  4. cornel Says:

    Andrew,

    I think that in the first method you forgot to decode the variables (that the purpose of using URLVariables class)

Leave a Reply