Tips: How To Get The SWF Path In ActionScript 3

presentblue-icon Sometimes we need to get the swf path in our flex/flash project, it can be done with _root._url in actionscript 2, but _root not work in actionscript 3. How to get the swf path in as 3? The following is 2 ways on how to get it.

Hope it will be useful for you.

Source code 1:

this.stage.loaderInfo.url

var doMain:String = this.stage.loaderInfo.url;
var doMainArray:Array = doMain.split("/");
if (doMainArray[0] == "file:") {
trace("Local debug mode.");
}else{
trace("Web host mode");
}

Source code 2:

import flash.external.ExternalInterface;
//Method 1
var urlPath = ExternalInterface.call("window.location.href.toString");
//Method 2
var urlPath = loaderInfo.url;

If you want to get the path of the HTML hosting the SWF, you can simply get it like:

import flash.external.ExternalInterface;
var full:String = ExternalInterface.call("window.location.href.toString");

Enjoy!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

2 Responses to “Tips: How To Get The SWF Path In ActionScript 3”

  1. Eric Holm says:

    This works for me:

    trace(loaderInfo.url);

  2. Thanks for this! It saved me some time in a crunch

Leave a Reply