Flex 3:从URL获取变量



如果我的应用程序位于http://sitename.com/myapp/我想通过url传递一个变量(即-http://sitename.com/myapp/?name=Joe),如何将名称变量转换为字符串变量?

我使用本文中提供的类Adobe。

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

更新:这个类的更新版本也可以在这里找到,尽管我还没有尝试过这个。

更新2:下面是一个关于如何使用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.");
  }
}

用String.split()分割em并征服:

    var url:String = "http://www.xxx.zzz/myapp?arg1=vae&arg2=victus";
    var params:String = url.substr(url.lastIndexOf("?") + 1);
    if (params.length == url.length) return; //no params
    for each (var pair:String in params.split("&"))
    {
        trace("Got parameter:");
        var nameValue:Array = pair.split("=");
        trace("name: " + nameValue[0] + ", value: " + nameValue[1]);
    }

输出:

Got parameter:
name: arg1, value: vae
Got parameter:
name: arg2, value: victus

相关内容

  • 没有找到相关文章

最新更新