2

today I'm trying to get the URL where my Flash movie is sitting on.

I found a similar question here, which was answered with a link to Flash's LoaderInfo method, but I'm not sure I'm using it correctly as the textField in my test movie here: http://leongaban.com/stackoverflow/getUrl/ does not display the URL

Updated! WORKING CODE

All I needed was this: stage.loaderInfo.url :)

package {

import flash.display.Stage;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import FontsTest;

public class LoaderInfoExample extends Sprite 
{
    private var myTextField :TextField = new TextField();
    private var urlIs:String = "";

    public function LoaderInfoExample() {

        urlIs = stage.loaderInfo.url;
        trace(stage.loaderInfo.url);
        addEventListener(Event.ADDED_TO_STAGE, initHandler);
    }

    private function initHandler(event:Event):void {

        myTextField.defaultTextFormat = FontsTest.Arial14Bold;
        myTextField.border = true;
        myTextField.antiAliasType = flash.text.AntiAliasType.ADVANCED;
        myTextField.selectable = true;
        myTextField.mouseEnabled = true;
        myTextField.autoSize = TextFieldAutoSize.LEFT;
        myTextField.text = "Url is = "+urlIs;

        addChild(myTextField);
    }
}
}

http://leongaban.com/stackoverflow/getUrl/

Community
  • 1
  • 1
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529

3 Answers3

7

If I'm understanding this correctly, you're doing it just a little bit wrong. You don't need to create a new loader - the .swf holds a reference to its own loaderInfo object on stage.

So if you want to trace out the current location of your .swf file, do something like this:

trace(stage.loaderInfo.url);

Does that help?

Myk
  • 6,205
  • 4
  • 26
  • 33
0

If its your own URL you're after, the DisplayObject class has a loaderInfo property you can use. Try this:

myLoader = this.root.loaderInfo;
urlIs = myLoader.loaderURL;

I'm relatively sure that loader object you're creating isn't going to have a loaderURL (or do anything else) until you tell it to load a request.

iandisme
  • 6,346
  • 6
  • 44
  • 63
0

You need to do an onAddedToStage instead of the Event.INIT

public function LoaderInfoExample() {
    addEventListener(Event.ADDED_TO_STAGE, initHandler);
}

and then use root.loaderInfo

Matti Lyra
  • 12,828
  • 8
  • 49
  • 67