Get URL of current page from Flex 3?

jsight picture jsight · Sep 15, 2009 · Viewed 20.2k times · Source

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

Answer

ccallendar picture ccallendar · Jun 21, 2011

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, "&");