flex浏览器相关辅组类

阅读更多
package util
{
	import flash.external.ExternalInterface;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.core.FlexGlobals;
	import mx.events.BrowserChangeEvent;
	import mx.managers.BrowserManager;
	import mx.managers.IBrowserManager;
	import mx.utils.URLUtil;

	/**
	 * A utility class related to browser
	 */ 
	public class BrowserUtil
	{
		private static var _singletonInstance:BrowserUtil;
		public var browserManager:IBrowserManager;
		public var fullURL:String;
		public var baseURL:String;
		public var fragment:String;
		public var protocol:String;
		public var port:int;
		public var serverName:String;
		public var isHttpsURL:Boolean;
		public var previousURL:String;
		public var contextRoot:String;
		
		public function BrowserUtil()
		{
			if(_singletonInstance!=null){
				throw new Error("Singleton class BrowserUtil has already been instantiated");
			}
			init();
		}
		
		private var _initalized:Boolean=false;
		private function init():void {
			if(!_initalized){
				browserManager = BrowserManager.getInstance();
				browserManager.addEventListener(BrowserChangeEvent.URL_CHANGE, urlChangedHandler);
				browserManager.addEventListener(BrowserChangeEvent.BROWSER_URL_CHANGE, browserURLChangeHandler);
				browserManager.addEventListener(BrowserChangeEvent.APPLICATION_URL_CHANGE,applicationURLChangeHandler);
				parseURL(getPageURL());
				browserManager.init("", "Welcome");
				browserManager.setTitle("Welcome");
				_initalized=true;
			}
		}
		
		private function getPageURL():String{
			var url:String=browserManager.url;
			if(url==null || url.length==0){
				url=getTopLevelApplication().url;
			}
			if(url==null || url.length==0){
				url=ExternalInterface.call("window.location.href.toString");
			}
			return url;
		}
		
		private function getTopLevelApplication():Object{
			//flex 3 use Application.application, flex 4 changed to
			return FlexGlobals.topLevelApplication;
		}
		
		private function browserURLChangeHandler(e:BrowserChangeEvent):void{
			//Alert.show("BrowserChangeEvent.BROWSER_URL_CHANGE triggered");
		}
		
		private function applicationURLChangeHandler(e:BrowserChangeEvent):void{
			//Alert.show("BrowserChangeEvent.APPLICATION_URL_CHANGE triggered");
		}
		
		public static function getInstance():BrowserUtil{
			if(_singletonInstance==null){
				_singletonInstance=new BrowserUtil();
			}
			return _singletonInstance;
		}
		
		public function updateTitle(title:String):void {
			browserManager.setTitle(title);
		}
		
		private function updateURL(fragment:String):void {
			browserManager.setFragment(fragment);
		}
		
		private function parseURL(url:String):void{
			baseURL = browserManager.base;
			fragment = browserManager.fragment;                
			fullURL = URLUtil.getFullURL(url, url);
			port = URLUtil.getPort(url);
			protocol = URLUtil.getProtocol(url);
			serverName = URLUtil.getServerName(url);
			isHttpsURL = URLUtil.isHttpsURL(url); 
			contextRoot=getContextRoot(fullURL);
		}
		
		private function urlChangedHandler(e:BrowserChangeEvent):void {
			var url:String = browserManager.url;
			parseURL(url);
			previousURL = e.lastURL;
		}
		
		/**
		 * Returns the context root of the application embedding this file. 
		 * Given http://localhost:8080/myapp/index.html, "myapp" will be returnd.
		 */ 
		private function getContextRoot(url:String):String{
			var firstDoubleSlash:int=url.indexOf("//");
			//find the first single slash, start 2 after firstDoubleSlash, note indexOf is 0-based method, so add 2
			var firstSingleSlash:int=url.indexOf("/",firstDoubleSlash+2)+1;
			var secondSingleSlash:int=url.indexOf("/",firstSingleSlash);
			return url.substring(firstSingleSlash,secondSingleSlash);
		}
	}
}

你可能感兴趣的:(flex.browser,url,protocol,context)