REQUEST_URI in Flex

Mar. 7, 2007 9 Comments Posted under: ActionScript, Flex

I recently found it very hard to discover the current URL in the browser from within my Flex aplication. I needed it so I knew which SOAP server to connect to depending on if the instance of the application was on dev, staging or live.

So in order to get the current URI I used this:


import flash.external.*; // Put this in the package

var currentUri:String = ExternalInterface.call("window.location.href.toString");

Then you can go about hacking it with string functions or regexp as you wish!

This entry was posted on Wednesday, March 7th, 2007 at 7:10 pm and is filed under ActionScript, Flex. You can leave a comment and follow any responses to this entry through the RSS 2.0 feed.

9 Comments Leave a comment

  1. Jot 3 April 2007 at 7:41 pm #

    would this work in flash? , when i try in flash8 AS2 i cant import the package without errors
    but:

    
    var currentUri:String = ExternalInterface.call("document.location.href.toString");
    

    gives me an error

    
    **Error** Scene=Scene 1, layer=Layer 3, frame=1:Line 6: Type mismatch in assignment statement: found Object where String is required.
         var currentUri:String = ExternalInterface.call("document.location.href.toString");
    

    if i change it to

    
    var currentUri:String then it returns an empty object.
    

    :/

  2. jot 3 April 2007 at 7:42 pm #

    damn, i meant:
    when i try in flash8 AS2 i _CAN_ import the package without errors

  3. jot 3 April 2007 at 7:43 pm #

    and i also meant to write
    “if i change it to var currentUri:Object”

    this is going well =/ sorry for meesing up.

  4. Rob 3 April 2007 at 10:16 pm #

    Try tracing:

    document.location.href

    and see what it returns. You shouldn’t be getting that error as you are saying .toString()

    I have never developed in flash though so can’t really help. Sorry

  5. Kevin 20 April 2007 at 7:19 pm #

    Of course, this only works if “allowScriptAccess” is not set to “never”. Which is where I find myself… :(

  6. Rob 20 April 2007 at 7:57 pm #

    Where would it be set to never?

  7. Arthur Clifford 10 May 2007 at 9:06 am #

    I happen to be addressing this same problem. I forget where I saw this trick, but this works for me (adapted for your variable names)

    var currentUri:String = String( ExternalInterface.call(” function(){ return document.location.href.toString();}”));

    So, the idea is that your call statement calls a function defined on the fly and returns the value you are interested in.

  8. Flash 中å?µæ¸¬åµŒå…¥é ?é?¢ç¶²å?€…

    Flash在內建æ??供的執行期間環境變數中,沒有直æ??供嵌入é ?é?¢ä¾†æº?ç¶²å?€è³‡è¨Šï¼Œ\r\n但還是å?¯ä»¥ç”¨ExternalInterface利用javascriptå?–å¾—é ?é?¢ç¶²å?€ã€‚\r\n如此便å?¯ä»¥å?šåˆ°åˆ¤æ–·é ?é?¢åµŒå…¥ä¾†æº?來決定是å?¦åŸ·è¡ŒF…

  9. phoenix 11 September 2009 at 8:30 pm #

    I have been on this referer issue for ages!!

    the :

    var url:String = String( ExternalInterface.call(” function(){ return document.location.href.toString();}”));

    worked a charm!! Im using as2 and flash 8..

    as3 is almost the same which is why it was killing me.

Leave a Reply