在Flex中获取URL参数值

时间:2011-07-18 19:42:05

标签: flex url parameters

我有以下网址:

www.example.com?arg1=foobar

我想获得arg1值。 我尝试使用以下代码:

var bm:IBrowserManager;
bm= bm.getInstance();
browserManager.init();
var o:Object = URLUtil.stringToObject(bm.fragment, "&");
Alert.show(o.arg1);

它适用于以下网址:

www.example.com#arg1=foobar

因此需要#而不是?

如何从Flex(很好地)从以下URL获取参数“arg1”:

www.example.com?arg1=foobar

谢谢;)

1 个答案:

答案 0 :(得分:1)

我刚才回答a similar question

我使用Adobe提供的in this article类。这个类的更新版本也可以be found here,虽然我没有尝试过这个。

  package 
  {
    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."); }
      }

    }
  }

以下是有关如何使用Querystring类的示例:

public function CheckForIDInQuerystring():void
{
  // sample URL: http://www.mysite.com/index.aspx?id=12345
  var qs:QueryString = new QueryString;

  if (qs.parameters.id != null)
  {   
    // URL contains the "id" parameter
    trace(qs.parameters.id);
  }
  else
  {                 
    // URL doesn't contain the "id" parameter
    trace("No id found.");
  }
}