Flex获取服务器url地址,域名,站点根目录等
这个类主要是flex获取当前所在网页url以及一些相关处理,比如获取当前swf文件所在的网页的url全路径,获取网页所在站点主机路径部分,获取网页所在站点的上下文路径,获取网页所在站点的根目录,获取当前url所带的全部参数等等。
package util { import flash.external.ExternalInterface; import mx.core.Application; import mx.utils.URLUtil; /** * 和当前网页的 url 相关的一些处理 */ public class URLUtilEx { /** * 获取当前 swf 文件所在的网页的 url 全路径 * eg: http://localhost:8080/web-tester/page/default.jsp?id=1 */ public static function getCurrentUrl():String{ var value:String = ExternalInterface.call("getCurrentUrl"); if(value == null){ value = ""; var msg:String = "call javascript function getCurrentUrl failed in this page."; msg += "\nfunction content:"; msg += "\nfunction getCurrentUrl(){ return window.location.href; }"; } return value; } /** * swf 应用程序的 url * eg: http://localhost:8080/web-tester/page/index.swf */ public static function getAppUrl():String{ var app:Application = Application.application as Application; return app ? app.url : ""; } /** * 获取当前网页所在站点主机路径部分 * eg: http://localhost:8080 */ public static function getRootUrl():String{ var url:String = getAppUrl(); return URLUtil.getProtocol(url) + "://" + URLUtil.getServerNameWithPort(url); } /** * 获取当前网页所在站点的上下文路径 * eg: /web-tester */ public static function getContextPath():String{ var url:String = getAppUrl(); var root:String = URLUtil.getProtocol(url) + "://" + URLUtil.getServerNameWithPort(url); var context:String = url.substr(root.length + 1); context = context.substring(0, context.indexOf("/")); return "/" + context; } /** * 获取当前网页所在站点的根目录 * http://localhost:8080/web-tester/ */ public static function getSiteRootUrl():String{ var out:String = ""; out += URLUtilEx.getRootUrl(); out += URLUtilEx.getContextPath() + "/"; return out; } /** * 获取当前 url 所带的全部参数,直接截取 ? 后面的字符串 * eg: a=1&b=2&c=leo */ public static function getQueryParams():String{ var url:String = getCurrentUrl(); var i:int = url.indexOf("?"); if(i >= 0){ return url.substring(i + 1); }else{ return ""; } } /** * 当前 url 是否有参数 */ public static function hasQueryParams():Boolean{ var params:String = getQueryParams(); if(params){ var ieq:int = params.indexOf("="); return (ieq >= 0); }else{ return false; } } /** * 根据当前网页的路径,将相对组装成一个完整路径。通过 URLUtil.getFullURL(rootUrl, url) 函数实现。 * * eg: 假如当前路径为 http://localhost:8080/web-tester/page/default.jsp ,将有下列结果 * eg1: /web-tester/page/default1.jsp --> http://localhost:8080/web-tester/page/default1.jsp * eg2: /page/default1.jsp --> http://localhost:8080/page/default1.jsp * eg3: default2.jsp --> http://localhost:8080/web-tester/page/default2.jsp */ public static function getFullUrl(relativeUrl:String, rootUrl:String=null):String{ if(rootUrl){ return URLUtil.getFullURL(rootUrl, relativeUrl); }else{ return URLUtil.getFullURL(getAppUrl(), relativeUrl); } } /** * swf 应用程序的 url * 假如:http://localhost:8080/web-tester/page/index.swf * 结果:http://localhost:8080/web-tester/page/ */ public static function getFlexPath():String{ var app:Array = URLUtilEx.getAppUrl().split("/"); var url:String = app[0] + "/"; for(var i:int = 1; i < app.length-1 ; i++){ url = url + app[i] + "/"; } return url; } } }