Search code examples
flashactionscript-3urlloaderinfo

How to obtain URL that Flash movie is embedded on?


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/


Solution

  • 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?