views:

164

answers:

3

Hello everyone, 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/

A: 

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
A: 

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
what import is needed so I can use the root keyword? And thanks for the tip!
Leon
root is part of the DisplayObject so extending Sprite should be enough.
Matti
+3  A: 

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
BAM! YES omg my brain was hurting... this is it!
Leon
Awesome, glad to help!
Myk