Passing request data with URL fragments

In applications, there are several ways to pass values to an application with the URL. You can convert query string parameters to flashVars variables or you can append them to the SWF file’s URL (for example, MyApp.swf?value1=x&value2=y). You do both of these things in the HTML wrapper that embeds the application. To access the values in your application, you then use the FlexGlobals.topLevelApplication.application object. For more information on passing flashVars variables, see Passing request data with flashVars properties.

The BrowserManager also provides a method of accessing values from a URL inside your application. You do this by accessing the URL fragments that deep linking uses. By default, you can append any Strings after the pound sign (“#”) in the URL and be able to access them as semi-colon-separated name/value pairs. For example, with a URL like the following, you can access the firstName and lastName values once you convert the fragment to an object:

http://www.mydomain.com/MyApp.html#firstName=Nick;lastName=Danger

To convert these parameters to an object, you get the URL and then use the URLUtil class’s stringToObject() method. You then access firstName and lastName as properties on that new object.

To make the URL more “URL-like”, you can separate the fragments with an ampersand (“&”). When you convert the fragment to an object with the stringToObject() method, you specify the new delimiter.

The following example takes a URL that sets the value of the firstName and lastName query string parameters in its fragment. It specifies an ampersand as the delimiter.

<?xml version="1.0" encoding="utf-8"?>
<!-- deeplinking/PassURLParamsAsFragments.mxml -->
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"
    creationComplete="init(event);">    

    <s:layout> 
        <s:VerticalLayout/> 
    </s:layout>

    <fx:Script>
    <![CDATA[
        import mx.managers.BrowserManager;
        import mx.managers.IBrowserManager;
        import mx.utils.URLUtil;

        private var bm:IBrowserManager;
        [Bindable]
        private var fName:String;
        [Bindable]
        private var lName:String;            

        private function init(e:Event):void {
            bm = BrowserManager.getInstance();                
            bm.init("", "Welcome!");

            /* The following code will parse a URL that passes firstName and lastName as
               query string parameters after the "#" sign; for example:
               http://www.mydomain.com/MyApp.html#firstName=Nick&lastName=Danger */
            var o:Object = URLUtil.stringToObject(bm.fragment, "&");                
            fName = o.firstName;
            lName = o.lastName;                
        }
    ]]>
    </fx:Script>
    
    <mx:Form>
        <mx:FormItem label="First name:">
            <mx:Label id="ti1" text="{fName}"/>
        </mx:FormItem>
        <mx:FormItem label="Last name:">
            <mx:Label id="ti2" text="{lName}"/>
        </mx:FormItem>
    </mx:Form>

</s:Application>