当前位置:首页 > 开发 > Web前端 > 前端 > 正文

flex浏览器相关辅组类

发表于: 2012-12-06   作者:darrenzhu   来源:转载   浏览次数:
摘要: package util { import flash.external.ExternalInterface; import mx.controls.Alert; import mx.core.Application; import mx.core.FlexGlobals; import mx.events.BrowserChangeEvent; import mx
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浏览器相关辅组类

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
摘要:由于IP资源的紧缺,NAT作为一种重要的解决方案在全球有着广泛的应用,但同时带来的一些问题也
http://www.ibm.com/developerworks/cn/web/0908_liuqing_flex_reflect/ Flex 作为新兴的 RIA 开发
Flex StringUtil工具类 最近发现Flex处理字符串的工具类一共有三个,分别是: mx.utils包下的String
http://www.ibm.com/developerworks/cn/web/0908_liuqing_flex_reflect/ Flex 作为新兴的 RIA 开发
HttpWebRequest 类对 WebRequest 中定义的属性和方法提供支持,也对使用户能够直接与使用 HTTP 的服
1、GenericServlet 如果我们直接通过实现Servlet接口来编写一个Servlet类,就需要实现接口中定义的5
public void getPixels(int[] pixels, int offset, int stride,int x, int y, int width, int heigh
自ActionScript 3开始,eval函数就被取消了,这样就不能像原来那样利用字符串动态的创建对象了,但
自ActionScript 3开始,eval函数就被取消了,这样就不能像原来那样利用字符串动态的创建对象了,但
自ActionScript 3开始,eval函数就被取消了,这样就不能像原来那样利用字符串动态的创建对象了,但
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号