Get URL of current page from Flex 3?
Asked Answered
H

7

6

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

Hydraulics answered 15/9, 2009 at 4:39 Comment(0)
D
2

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

Dorella answered 15/9, 2009 at 10:18 Comment(1)
This is not the correct answer, as in most circumstances you are not allowed to execute JavaScript (for example: Kongregate hosted flash.)Chessboard
B
23

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, "&");
Buna answered 20/6, 2011 at 23:8 Comment(0)
H
8

From the Application:

var myUrl:String = Application.application.url;
Hydraulics answered 16/9, 2009 at 5:21 Comment(0)
M
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! :)

Mobster answered 15/9, 2009 at 4:48 Comment(0)
D
2

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

Dorella answered 15/9, 2009 at 10:18 Comment(1)
This is not the correct answer, as in most circumstances you are not allowed to execute JavaScript (for example: Kongregate hosted flash.)Chessboard
M
0

From the Application, use: this.loaderInfo.loaderURL

to break it apart and use parts of it do:

var splitURL:Array = this.loaderInfo.loaderURL.split('/');
var baseURL:String = "http://"+splitURL[2];
Matteroffact answered 16/9, 2009 at 23:49 Comment(1)
I suggest using URLUtil for this purpose: help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/…Pelorus
A
0

I tried the e:BrowserChangeEvent version and inside my class it didnt or wasnt the appropriate moment for this event to work so in short it didn't work !

Using Application.application.loaderInfo.loaderURL is my preferred solution.

Abroach answered 28/3, 2011 at 16:17 Comment(0)
S
0
ExternalInterface.call("window.location.href.toString");
Statehood answered 5/3, 2012 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.