Avg. Rating 2.0

Problem

How to get query-string from URL into your flex application?

Solution

QueryString class extracts the query-string from URL and returns the parameters through easy to use API.

Detailed explanation

I wrote a quick-and-dirty class in ActionScript 3, which would get the values of params from URL query-string for you. You don't need any extra JavaScript code in HTML. The same logic can be used for any an ActionScript 2.0 running Macromedia Flash Player 8.

QueryString class has following properties:-

 

  • parameters - an Object which contains the name-value pairs from query-string
  • queryString- String, this contains the entire query-string (url-encoded) name-value pairs.
  • url- String, this returns the complete URL of the wrapper page with query-string.

 

Source of QueryString class:


 
package com.abdulqabiz.utils 
  {
    import flash.external.*;
    import flash.utils.*;

    public class QueryString 
    {

      private var _queryString:String;
      private var _all:String;
      private var _params:Object;

      public function get queryString():String
      {
        return _queryString;
      }
      public function get url():String
      {
        return _all;
      }
      public function get parameters():Object
      {
        return _params;
      }           


      public function QueryString()
      {

        readQueryString();
      }

      private function readQueryString():void
      {
        _params = {};
        try 
        {
          _all = 
  ExternalInterface.call("window.location.href.toString");
          _queryString =
  ExternalInterface.call("window.location.search.substring", 1);
          if(_queryString)
          {

            var params:Array = _queryString.split('&');
            var length:uint = params.length;

            for (var i:uint=0,index:int=-1; i 0)
              {
                var key:String = kvPair.substring(0,index);
                var value:String = kvPair.substring(index+1);
                _params[key] = value;
              }
            }
          }
        }catch(e:Error) { trace("Some error occured.
  ExternalInterface doesn't work in Standalone player."); }
      }

    }
  }
  



 

Example Usage:

 

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="initVars()">

    <mx:Script><![CDATA[



        import com.abdulqabiz.utils.QueryString;



        // Declare bindable properties in Application scope.

        [Bindable]

        public var myName:String;

        [Bindable]

        public var myHometown:String;

        

        private var qs:QueryString;



        // Assign values to new properties.

        private function initVars():void {

           qs = new QueryString();

            name_lbl.text = qs.parameters.myName;

            homeTown_lbl.text = qs.parameters.myHomeTown;

           trace(qs.url);

        }

    ]]></mx:Script>

    

    <mx:VBox>

    <mx:HBox>

        <mx:Label text="Name: "/>

        <mx:Label id="name_lbl"  fontWeight="bold"/>

    </mx:HBox>

    <mx:HBox>

        <mx:Label text="Hometown: "/>

        <mx:Label id="homeTown_lbl" fontWeight="bold"/>

    </mx:HBox>

    </mx:VBox>

</mx:Application>

 

Note: This solution was posted on March 6th, 2006 on over here on my blog. Some developers have improve the code of QueryString class to use latest facilities provided in Flex framework and Flash Player. You might want to check those by reading the comments on my blog post.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes