flex接收处理url后跟的参数
首先解释下本文要讨论的内容。。。
flex开发生成的网页有如下形式
http://myserver/flexdev/index.html
这是最常见的一种,其实这个url后面可以跟参数,比如
http://myserver/flexdev/index.html?username=111&password=222
在flex程序中如何接收这些传递过来的参数值?这是这篇博文要讨论的有两种方式可以接收客户端传递过来的参数
方式一:
var userName:String = FlexGlobals.topLevelApplication.parameters.username;
var password:String = FlexGlobals.topLevelApplication.parameters.password;
if (userName && password)
{
......
}
var password:String = FlexGlobals.topLevelApplication.parameters.password;
if (userName && password)
{
......
}
方式二:
private function setUrlParams():Ojbect
{
var urlParams:Object = {};//诸如{username:111, password:222}的url参数键值对
if (!ExternalInterface.available)
{
return urlParams;
}
try
{
var queryStringFromUrl:String = ExternalInterface.call("window.location.search.substring", 1);//得到username=111&password=222
if (queryStringFromUrl)
{
var params:Array = queryStringFromUrl.split('&');
var length:uint = params.length;
for (var i:uint = 0, index:int = -1; i < length; i++)
{
var kvPair:String = params[i];
if ((index = kvPair.indexOf("=")) > 0)
{
var key:String = kvPair.substring(0, index);
var value:String = kvPair.substring(index + 1);
if (key)
{
urlParams[key] = decodeURIComponent(value);
}
}
}
}
}
catch (error:Error)
{
Alert.show("Some error occurred. ExternalInterface doesn't work in Standalone player.");
}
return urlParams;
}
var urlParams:Object = setUrlParams();
var userName:String,password:String;
if (urlParams.username)
{
userName = urlParams.username;
}
if (urlParams.password)
{
password = urlParams.password;
}
if (userName && password)
{
......
}
{
var urlParams:Object = {};//诸如{username:111, password:222}的url参数键值对
if (!ExternalInterface.available)
{
return urlParams;
}
try
{
var queryStringFromUrl:String = ExternalInterface.call("window.location.search.substring", 1);//得到username=111&password=222
if (queryStringFromUrl)
{
var params:Array = queryStringFromUrl.split('&');
var length:uint = params.length;
for (var i:uint = 0, index:int = -1; i < length; i++)
{
var kvPair:String = params[i];
if ((index = kvPair.indexOf("=")) > 0)
{
var key:String = kvPair.substring(0, index);
var value:String = kvPair.substring(index + 1);
if (key)
{
urlParams[key] = decodeURIComponent(value);
}
}
}
}
}
catch (error:Error)
{
Alert.show("Some error occurred. ExternalInterface doesn't work in Standalone player.");
}
return urlParams;
}
var urlParams:Object = setUrlParams();
var userName:String,password:String;
if (urlParams.username)
{
userName = urlParams.username;
}
if (urlParams.password)
{
password = urlParams.password;
}
if (userName && password)
{
......
}