Flex Pasta » Getting URL Parameters, host name, port, in Flex, Actionscript
Getting URL Parameters, host name, port, in Flex, Actionscript
So you’re coding up your flex app and you want to pass a url parameter into your application. Like getting the color parameter from mydomain.com?color=red. While one option is to call the built in flex option of Application.application.parameters, this will only work if your example points directly to the swf. If your url is main.swf?color=red then Application.application.parameters.color will work fine for you. However, if like most people, have the swf wrapped in an html page(main.html?color=red), this approach won’t work. I ran into this problem myself, and I didn’t want to get into the messy business of editing the flash vars, especially if I had a large number of parameters or wanted to add more on the fly later. I created a HTTPUtil class in actionscript to get the url information. The class uses javascript to get all of the url information, including host name, port, and url parameters among other things. The most helpful method you will find is getParameterValue(key:String):String which will give you back the parameter value for the given key.
Have multiple environments and need to get the full url with the host name and port in actionscript?
There are several methods to help with this:
getHostName()
getPort()
getProtocol() -such as http or https
getContext() -The path after the hostname but before the url parameters
Attached is the source. You can see that the methods are all static and use javascript to get the information back to flex. It is designed to get this information regardless of where the flex swf file lives since most people have them embeded in an html file.
7 Comments
1. Dirk replies at 1st April 2008, 10:28 am :
btw, there’s also mx.utils.URLUtil which also offers some helpful functions.
2. Jim replies at 1st April 2008, 8:06 pm :
True, however, it lacks functionality for retrieving the current url information.
3. Gustav replies at 25th April 2008, 2:46 pm :
Looks interesting! Is this affected by how the “allowScriptAccess” is set in the embed/object-tag?
Thanks!
4. Brian Telintelo replies at 30th April 2008, 7:58 am :
Yes, you have to allow javascript access.
5. lexa replies at 22nd July 2008, 7:25 am :
Hi Jim
you could expose a function which would return an object representing all the params passed in URL :
public static function getHtmlParameters():Object
{
// retrieve the querry string
var uparam:String = ExternalInterface.call("window.location.search.toString");
if(uparam==null)
{
return null;
}
// build the parameter+value pairs array
var paramArray:Array = uparam.split('&');
// build the parameters object
var paramsObject:Object = new Object;
for(var x:int=0; x<paramArray.length; x++)
{
// split the name/value pair on "="
var splitArray:Array = paramArray[x].split('=');
// retrieve name and value for this parameter
var name:String = splitArray[0];
var value:String = splitArray[1];
// adds the parameter to the result object
paramsObject[name] = value;
}
// returns the result object
return paramsObject;
}
6. Ryan replies at 27th October 2008, 1:37 pm :
When I use your code I get an error that says I cannot nest packages. I have no idea how to remedy this. Please could you assist me.
Regards
Ryan Duminy
7. Brian Telintelo replies at 29th October 2008, 7:52 pm :
There is probably something wrong with the package you place the class file. The easiest way to fix this problem is to place the cursor at the end of the line with the package declaration and press ctrl+space and flexbuilder will correct the package name.
Leave a comment