How to get an SWF to recognize its own filename (Actionscript 3)

I needed to recreate my previous post about finding the filename for a swf’s main timeline for AS3. The problem stemmed from the removal of the “_root” and “_url” properties in Actionscript 3.

Here’s the revised code:’

var myFileName:String;
var myFileNameArray:Array = new Array();
var num:Number;
var thisSWF:String;

// get this SWF's filename
myFileName = this.loaderInfo.url;
myFileNameArray = myFileName.split("/");
num = myFileNameArray.length;

thisSWF = myFileNameArray[num-1];

12 thoughts on “How to get an SWF to recognize its own filename (Actionscript 3)

  1. Thanks! I was looking for a way to do this in as3. Most of my files have underscores and/or hyphens which flash outputs with encoded characters.

    Ultimately I ended up with this, which has been working well so far:

    var swf:String = this.loaderInfo.url.split(“/”).pop().replace(“%5F”, “_”).replace(“%2D”, “-”);

  2. You dont have to get the length of the Array to get the last item.
    Just use the shift() method.

    // get this SWF’s filename
    myFileName = this.loaderInfo.url;
    myFileNameArray = myFileName.split(“/”);

    thisSWF = myFileNameArray.shift();

  3. This is great but only replaces the first instance of an underscore or dash. Try this instead to replace all instances:

    var swf:String = loaderX.url.split(“/”).pop().replace(/%5F/g, “_”).replace(/%2D/g,”-”);


  4. @ditchhopper
    Nice one, but only first instance of replacement string is changed. To change all of it you need to define regular expression.

    var myFileName:String = this.loaderInfo.url.split(“/”).pop().replace(RegExp(/%20/gi), ” “);

    Where gi is: g – global (all instances), i – ignore case

  5. Hi,
    Modifyed the code a little

    Here is my class:

    package {

    import flash.display.DisplayObjectContainer;

    public class GetOwnName {

    private var _fileName:String;
    private var fullPath:String;
    private var lastIndex:uint;

    public function GetOwnName(place:DisplayObjectContainer):void {

    fullPath = place.loaderInfo.url;
    lastIndex = fullPath.lastIndexOf(“/”);

    _fileName = fullPath.slice(lastIndex+1,fullPath.length).split(“%5F”).join(“_”).split(“%2D”).join(“-”);


    public function get fileName():String {

    return _fileName;




  6. Pingback: Joaquin Jolin
  7. Pingback: Aliya May
  8. Just to let you know regex is not really the right way to go here, especially because AS3 has it all built in.

    var filename:String = decodeURIComponent(this.loaderInfo.url.split(“/”).pop());

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>