Here’s a handy tip which can help you when deploying Flex applications on mulitple servers (such as a staging/production server). Basically you can listen for the Application tag to dispatch the applicationComplete event, grab the URL of the SWF using the loaderInfo.url property, and then use the URLUtil.getServerName() method to parse out the server name.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/09/determining-which-serverdomain-a-swf-is-hosted-on/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" applicationComplete="init()">
<mx:Script>
<![CDATA[
import mx.utils.URLUtil;
private function init():void {
lbl.text = "url=" + URLUtil.getServerName(Application.application.loaderInfo.url);
}
]]>
</mx:Script>
<mx:Label id="lbl" text="" />
</mx:Application>
View source is enabled in the following example.





Hi,
what kind of dataType is returned from the loaderInfo.url? Must be a String…isn’t it?
Charly,
Yep, its a String. If I would have displayed the full URL, it would have shown:
http://blog.flexexamples.com/wp-content/uploads/Application_loaderInfo_url_test/bin/main.swf
Although I used the
URLUtil.getServerName()method to just return the server name, or just “blog.flexexamples.com”.Couldn’t you also just use Application.application.url ?
Dasa,
Yes, I believe you can use
Application.application.urlif you just have one main SWF (Application.application.urlandApplication.application.loaderInfo.urlshould return the same value), but if you’re loading another Flex SWF using the SWFLoader control, I believe they start returning different values.For example, in the loaded SWF, I believe
Application.application.loaderInfo.urlreturns the current losded SWF file’s URL, whereasApplication.application.urlreturns the parent SWFs URL.At least thats what I think.
Thanks for this one peterd - I’d been scouring around all morning trying to find a solution like this!
peterd,
Very handy. Does URLUtil has functions to get referer and user agent, similar to PHP’s $_SERVER[’HTTP_REFERER’] and $_SERVER[’HTTP_USER_AGENT’]? Thank you.
Wayne
Wayne,
I don’t believe it does, but you could use the ExternalInterface API to get that information from your HTML container using JavaScript. For example, see “Returning values from JavaScript in your Flex applications using the ExternalInterface API”.
Peter
Thanks for this solution peterd.
Congrats for the post.
Charles, www.tldsco.com