Get URL of current page from Flex 3?

20,305

Solution 1

Using ExternalInterface (flash.external.ExternalInterface), you can execute Javascript in the browser. Knowing this, you can call

ExternalInterface.call("window.location.href.toString");

to get the current URL (note that this will be the page url and not the .swf url).

hth

Koen

Solution 2

Let's be clear here.

1. If you want the URL of the loaded SWF file, then use one of these.

Inside your application:

this.url;

From anywhere else:

Application.application.url; // Flex 3
FlexGlobals.topLevelApplication.url; // Flex 4

If you are loading your SWF inside another SWF, then keep in mind that the code above will give different values. this.url will return the url of your SWF, where as Application.application.url will give the url of the parent/root SWF.

2. If you want to know the URL that is in the browser address bar, then use one of these.

BrowserManager method(Make sure you have the History.js included in your wrapper html for this to work):

var browser:IBrowserManager = BrowserManager.getInstance(); 
browser.init();
var browserUrl:String = browser.url; // full url in the browser
var baseUrl:String = browser.base; // the portion of the url before the "#"
var fragment:String = browser.fragment; // the portion of the url after the "#"

JavaScript method:

var browserUrl:String = ExternalInterface.call("eval", "window.location.href");

If you are parsing the url for parameters, don't forget about this useful function:

// parses a query string like "key=value&another=true" into an object
var params:Object = URLUtil.stringToObject(browserURL, "&");

Solution 3

From the Application:

var myUrl:String = Application.application.url;

Solution 4

I searched and came up with this url. I've honestly never used Flex, but it looks like the important part of that document is this:

private function showURLDetails(e:BrowserChangeEvent):void {
  var url:String = browserManager.url;
  baseURL = browserManager.base;
  fragment = browserManager.fragment;                
  previousURL = e.lastURL;                

  fullURL = mx.utils.URLUtil.getFullURL(url, url);
  port = mx.utils.URLUtil.getPort(url);
  protocol = mx.utils.URLUtil.getProtocol(url);
  serverName = mx.utils.URLUtil.getServerName(url);
  isSecure = mx.utils.URLUtil.isHttpsURL(url);        
}

Either way, good luck! :)

Share:
20,305
caseyamcl
Author by

caseyamcl

I'm a Java and .Net programmer.

Updated on December 03, 2020

Comments

  • caseyamcl
    caseyamcl over 3 years

    How do I determine the URL of the current page from within Flex?