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.
19 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.
8. Ramdas replies at 27th January 2009, 7:40 am :
That was helpful, thanks a lot…
9. João Paulo Braga replies at 29th January 2009, 10:13 am :
Thank you very much, your piece of code it is really useful!
10. Sunny replies at 6th February 2009, 12:16 pm :
I tried using ExternalInterface.call(”window.location.search.toString”); in bith AS3 and 2 but all I get back is a “null” no idea whats going on. any insight please?
Thanks
11. Brian Telintelo replies at 8th February 2009, 10:33 am :
Do you have an html wrapper file around your swf file? What browser?
12. ssin replies at 1st June 2009, 5:39 am :
Hi,
Do you have examples or sample code as to how this class can be used?
Thanks
13. bob replies at 24th August 2009, 2:25 am :
I have question :
There is swf in an iframe, and iframe’s src is not same domain with the iframe parent’s url . How can I get the iframe parent’s url in swf.
14. gabriel replies at 15th October 2009, 11:36 am :
Hello Brian,
Very handy.
How’s this class licensed? Can we use it in commercial projects?
Cheers.
15. Dan G replies at 16th October 2009, 4:44 pm :
Nice work. I’d also like to know about the license and if it can be used in commercial projects.
Thanks
16. amit chauhan replies at 18th October 2009, 4:06 am :
public static function getHtmlParameters(querystring:String):String
{
// retrieve the querry string
var uparam:String = ExternalInterface.call(”window.location.search.toString”);
if(uparam==null)
{
return null;
}
else
{
uparam=uparam.replace(”\?”,”&”);
}
// 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
if (querystring== name) {return value;}
}
// returns the result object
return “”;
}
17. amit chauhan replies at 18th October 2009, 4:07 am :
modified original function so u get the parameter via function call
even 1st parameter is returned now.:D
18. Bryan replies at 23rd January 2010, 1:16 pm :
Have you tried this on a swf accessed through IE 8?
19. Immanuel replies at 10th April 2010, 2:53 pm :
Nice stuff
Leave a comment