URL paramaters in Flex

I had a very interesting question last week about accessing URL parameters from within a Flex application. It turns out that it is much easier than I expected! You just need to reference the URL parameter as a variable and away you go. It is maggically is created for you.

You may want to watch out though, if your application references a URL variable and it does not exist, the compiler will throw a warning as it does not know what your variable is, it is most probably a good idea to declare this with the rest of your variables. Secondly, they are only available at the top level of your application, not inside your various other components, so you will need to pass them around, just like any other variable.

Here is a little code snippet that goes looking for a URL parameter called 'name'

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Script>
<![CDATA[
   var userid:Object = "nothing was passed in";
]]>
</mx:Script>
<mx:Label text="{userid}" />
</mx:Application>

On a little side note, don't call your mxml file 'getURL.mxml' like I did. When you an app called this, Flash thinks you mean the function and instantly trys to get a URL that is not defined in the current browser window. Doh!

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.9.4.001. Contact Blog Owner