[AS] SWF가 위치한 폴더 경로 반환 함수

작성자

카테고리:

/*
* getFolderPath
* 무비클립이 위치한 폴더의 경로를 절대경로로 반환하는 함수
* @mc : 폴더의 경로를 알고자하는 무비클립
* @subFolder : 타겟 폴더의 경로
*/
getFolderPath = function(mc:MovieClip, subFolder:String):String{
        var url:String = mc._url;

        var protocol:String;
        var domain:String;
        var path:String;

        // set protocol
        if(url.indexOf("http://") >= 0){
                // web
                protocol = "http://";
        }else{
                // local
                protocol = "file:///";
        }

        var urls:Array = url.split(protocol);
        var paths:Array = urls[1].split("/");

        // set domain
        domain = paths.shift().toString() + "/";

        // remove filename
        paths.pop();

        // analyze subfolder structure
        if(subFolder == undefined){
                // none
                subFolder = "";

        }else if(subFolder.indexOf("/") == 0){
                // root
                paths = [];
                subFolder = subFolder.slice(1);

        }else if(subFolder.indexOf("../") == 0){
                // parent folder
                var subs:Array = subFolder.split("../");
                var count:Number = subs.length-1;
                for(var i:Number=0; i= 0){
                        subFolder = subFolder.slice(2);
                }
        }// end if

        // set path
        path = paths.join("/");

        // make full path
        url = protocol + domain + path;

        // add sub folder to full path
        if(url.lastIndexOf("/") != url.length-1){
                url += "/";
        }// end if
        url += subFolder;

        // confirm if "/" is last character
        if(url.lastIndexOf("/") != url.length-1){
                url += "/";
        }// end if

        return url;
}// getFolderPath

전에 만들었던 함수를 개선하여, 매개변수로 하위 폴더 정보를 추가할 수 있도록 수정한 버전….

getFolderPath(this);
는 물론이고
getFolderPath(this, "./subfolder");
getFolderPath(this, "sub/subfonder");
getFolderPath(this, "../../../");
getFolderPath(this, "../../subfolder");
getFolderPath(this, "/");
getFolderPath(this, "/fubfolder");
와 같이 원하는 폴더구조를 입력하여 사용할 수 있습니다.