09
Aug
07

Determining which server/domain a SWF is hosted on

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.

View MXML

<?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.


10 Responses to “Determining which server/domain a SWF is hosted on”


  1. 1 Charly Aug 10th, 2007 at 12:26 am

    Hi,

    what kind of dataType is returned from the loaderInfo.url? Must be a String…isn’t it?

  2. 2 peterd Aug 10th, 2007 at 6:29 am

    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”.

  3. 3 Dasa Aug 10th, 2007 at 7:54 am

    Couldn’t you also just use Application.application.url ?

  4. 4 peterd Aug 10th, 2007 at 8:30 am

    Dasa,

    Yes, I believe you can use Application.application.url if you just have one main SWF (Application.application.url and Application.application.loaderInfo.url should 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.url returns the current losded SWF file’s URL, whereas Application.application.url returns the parent SWFs URL.

    At least thats what I think.

  5. 5 Dale Feb 21st, 2008 at 9:52 am

    Thanks for this one peterd - I’d been scouring around all morning trying to find a solution like this!

  6. 6 Wayne Apr 4th, 2008 at 4:39 pm

    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

  7. 7 peterd Apr 4th, 2008 at 6:13 pm

    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

  8. 8 Charles May 2nd, 2008 at 6:29 am

    Thanks for this solution peterd.
    Congrats for the post.

    Charles, www.tldsco.com

  9. 9 jlafferty Aug 22nd, 2008 at 3:13 pm

    Thanks PD. I did a quick web search because I forgot how to do this and you had the answer. :)

  10. 10 peterd Aug 22nd, 2008 at 3:19 pm

    jlafferty,

    You are welcome. That will be $3 please.

    Peter

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed