Fx{r} is trying to start the Fx{r} Community! Please join our group on Adobe Groups following this link: http://groups.adobe.com/groups/ab29539ab9.
Fx{r} is now on Twitter too. Follow us @ twitter.com/fx_r!
«
»

ActionScript

Access the Query-String Variables from Flex Application

Andrei Ionescu | 28.11.07 | 7 Comments

Google Buzz

Working on a project we arrived at a point where we needed to read inside the flex application the variables set in the query string. But in the SWF case we have two situations: 1) the query string is in the URL (like yoursite.com/page.php?param1=aaa) and 2) the query string is in the embedded SWF Object (like app.swf?param2=bbb or added using the Javascript code created by Flex Builder).

1) The First case: the query string is in the URL

For this case we access the URL through the Javascript as bellow:

ExternalInterface.call("window.location.search.substring", 1);

After that we need to split the string by “&” and “=” to get the variable/value pairs.

1) The Second case: the query string is in the embedded SWF Object

In this case we have a much simpler situation. Using:

mx.core.Application.application.parameters

we get all we need. You will notice that other parameters are present.

The code of the whole class follows:

import flash.external.*;
import flash.utils.*;
import mx.core.Application;
 
public class QueryString
{
 
    private var _queryStringFromUrl:String;
    private var _paramsFromUrl:Object;
    private var _noOfParamsFromUrl:uint;
 
    private var _noOfParamsFromSwfObject:uint;
    private var _paramsFromSwfObject:Object;
 
    private var _params:Object;
    private var _noOfParams:uint;
 
    /**
     * getters and setters for the general array containing both
     * params from URL and from SWF Object
     **/
    public function get parameters():Object
    {
        return _params;
    }
    public function get noOfParams():uint
    {
        return _noOfParams;
    }	
 
    public function QueryString()
    {
        readQueryString();
        getParamsFromSwf();
        combineParams();
    }
 
    /**
     * method used to read parameters from SWF Object
     **/
    private function getParamsFromSwf():void
    {
        _paramsFromSwfObject = mx.core.Application.application.parameters;
    }
 
    /**
     * method used to read URL query string
     **/
    private function readQueryString():void
    {
        _paramsFromUrl = {};
        try
        {
            _queryStringFromUrl = ExternalInterface.call("window.location.search.substring", 1);
            if(_queryStringFromUrl)
            {
                var params:Array = _queryStringFromUrl.split('&');
                var length:uint = params.length;
                _noOfParamsFromUrl = length;
 
                for (var i:uint=0,index:int=-1; i<length; i++)
                {
                    var kvPair:String = params[i];
                    if ((index = kvPair.indexOf("=")) > 0)
                    {
                        var key:String = kvPair.substring(0,index);
                        var value:String = kvPair.substring(index+1);
                        _paramsFromUrl[key] = value;
                    }
                }
            }
        }
        catch(e:Error)
        {
            trace("Some error occured. ExternalInterface doesn't work in Standalone player.");
        }
    }
 
    /**
     * method used to combine the 2 associative arrays
     **/
    private function combineParams():void
    {
        /*
         * Remove the duplicates from url because the params from SWF Object has priority
         * Put the new associative array in the newParamsFromUrl
         */
        var newParamsFromUrl:Object = new Object();
 
        for (var tmpC1:* in _paramsFromUrl)
        {
            for (var tmpC2:* in _paramsFromSwfObject)
            {
                if(tmpC1 == tmpC2)
                {
                    // do nothing - pass over to eliminate the duplicate
                } else {
                    newParamsFromUrl[tmpC1] = _paramsFromUrl[tmpC1];
                }
            }
        }
 
        /*
         * Combine the _paramsFromSwfObject with newParamsFromUrl
         * No duplicates from this point on
         * The new array will be accesible throug _params variable and it's getter
         */
        _params = _paramsFromSwfObject;
        for (var tmpC3:* in newParamsFromUrl)
        {
            _params[tmpC3] = newParamsFromUrl[tmpC3];
        }
 
        /*
         * Count the number of parameters
         */
        _noOfParams=0;
 
        for (var tmpC4:* in _params)
        {
            _noOfParams++;
        }
    }
}

You can use the class like this:

_queryString = new QueryString();
_parameter1 = _queryString.parameters['param1'];

where param1 is the varaible from query-string.

Inspiration came from here but the final class concatenates both parameters from URL and SWF-Object and puts them in one array without duplicates (the parameters in from SWF-Object have overwrites the ones from URL).

Share and Enjoy:
  • Twitter
  • Google Buzz
  • LinkedIn
  • Google Bookmarks
  • del.icio.us
  • Digg
  • Sphinn
  • blogmarks
  • Reddit
  • StumbleUpon
  • Facebook
  • DZone
  • FriendFeed
  • Yahoo! Buzz
  • Yahoo! Bookmarks
  • Slashdot
  • MySpace
  • Add to favorites




Tags: , , ,

This post was written by Andrei Ionescu

Views: 12434

related

7 Comments

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

:

:


«
»