R – How to get the absolute path of the swf file in Actionscript

actionscriptflash

I would like to get the absolute path of my swf file from within Actionscript.

E.g. if the script called "http://www.mysite.com/banner/flash.swf" I'd expect "/banner"

In PHP I would do:

$fpath = str_replace('\\', '/', dirname(__FILE__));
$path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $fpath);

How can I do this in Actionscript?

Best Answer

I use this function in the top most class (the one that extends Sprite for AS3 projects or mx:Application for Flex projects).

    private function GetURLParts():Object
    {
        var urlPattern:RegExp = /([\w]+):\/\/([\w\._-]+)+(\S+)*(\?\S+)?/;
        var result:Array = urlPattern.exec(loaderInfo.loaderURL);

        var parts:Object = 
        {
            'protocol': result[1],
            'domain': result[2],
            'path': result[3]
        };

        return parts;
    }

You could probably modify this to fit your needs.

Related Topic