在AS项目中获取当前项目编译的SWF所在的服务器路径或本地路径
package { import flash.display.Sprite; public class HelloWorld extends Sprite { public static var currSwfUrl:String; public function HelloWorld(){ //写网络相关的项目时,就可以通过这个自动选择调用的服务器端程序了 var doMain:String = this.stage.loaderInfo.url; var doMainArray:Array = doMain.split("/"); if (doMainArray[0] == "file:") { //为处理本地系统返回的路径由“/”或“”两种间隔组成的不同情况,而分别处理 if(doMainArray.length<=3){ currSwfUrl = doMainArray[2]; currSwfUrl = currSwfUrl.substring(0,currSwfUrl.lastIndexOf(currSwfUrl.charAt(2))); }else{ currSwfUrl = doMain; currSwfUrl = currSwfUrl.substring(0,currSwfUrl.lastIndexOf("/")); } }else{ currSwfUrl = doMain; currSwfUrl = currSwfUrl.substring(0,currSwfUrl.lastIndexOf("/")); } currSwfUrl = currSwfUrl + "/"; trace("项目路径: "+ HelloWorld.currSwfUrl); } } }
在Flex Web项目中获取当前项目编译的SWF所在的服务器路径或本地路径
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="initApp()"> <mx:Script> <![CDATA[ public static var currSwfUrl:String; private function initApp():void{ //写网络相关的项目时,就可以通过这个自动选择调用的服务器端程序了 var doMain:String = this.parent.stage.loaderInfo.url;//注意此行和AS的写法略有不同 var doMainArray:Array = doMain.split("/"); if (doMainArray[0] == "file:") { if(doMainArray.length<=3){ //为处理本地系统返回的路径由“/”或“”两种间隔组成的不同情况,而分别处理 currSwfUrl = doMainArray[2]; currSwfUrl = currSwfUrl.substring(0,currSwfUrl.lastIndexOf(currSwfUrl.charAt(2))); }else{ currSwfUrl = doMain; currSwfUrl = currSwfUrl.substring(0,currSwfUrl.lastIndexOf("/")); } }else{ currSwfUrl = doMain; currSwfUrl = currSwfUrl.substring(0,currSwfUrl.lastIndexOf("/")); } currSwfUrl += "/"; trace("项目路径: "+ HelloFlex.currSwfUrl); } ]]> </mx:Script> </mx:Application>
JS 获取当前页面的路径和名称:
// 取当前页面名称(不带后缀名)
function pageName()
{
var a = location.href;
var b = a.split("/");
var c = b.slice(b.length-1, b.length).toString(String).split(".");
return c.slice(0, 1);
}
//取当前页面名称(带后缀名)
function pageName()
{
var strUrl=location.href;
var arrUrl=strUrl.split("/");
var strPage=arrUrl[arrUrl.length-1];
return strPage;
}