开发者

How to obtain URL that Flash movie is embedded on?

开发者 https://www.devze.com 2022-12-16 20:13 出处:网络
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 cor

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/


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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号