Getting url parameters in Flex

Recently I needed to access url parameters from Flex code. Here is an example how to do it. I am using ExternalInterface to get access to the browser (you cannot get parameters from Application.application.url).
Pay attention to the following note from doc:

Note: When embedding SWF files within an HTML page, make sure that the id and name attributes of the and tags do not include characters such as: . (period), -, +, *, /, and \.

I would also add additional character: ‘{’ - by mistake I use application name ‘app{’ and because of this could not access ExternalInterface (any method invocation returns null in this case)

You can use the following code to parse url and get dictionary that contains param names as key and param values as values.


function getUrlParamateres():Dictionary
{
   var urlParams:Dictionary = new Dictionary(); 

   var fullUrl:String = ExternalInterface.call(’eval’,'document.location.href’); 

   var paramStr:String = fullUrl.split(’?')[1];
   if (paramStr != null)
    {
        var params:Array = paramStr.split(’&’);
        for (var i:int = 0; i < params.length; i++)
       {
            var kv:Array = params[i].split(’=');
            urlParams[kv[0]] = kv[1];
        }
    } 

    return urlParams;
}

Note: Probably better to use regex to replace parsing code to more simple statement (as long as regex is not too complex).


About this entry


  1. Abdul Qabiz 04.02.07 / 4am

    I wrote a class long time back in AS3/Flex2 to do that, you might want to check that.

    I am doing the same thing you are doing here..

    http://www.abdulqabiz.com/blog/archives/macromedia_flex/how_to_get_url.php

    -abdul

Have your say

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>




Safari hates me