javascript之event组件封装

__klogger.event = (function(){
	// http://dean.edwards.name/weblog/2005/10/add-event/
	// a counter used to create unique IDs
    var guid = 1;
    function _addEvent(element, type, handler) {
		if (element.addEventListener) {
			element.addEventListener(type, handler, false);
		} else {
			// assign each event handler a unique ID
			if (!handler.$$guid) handler.$$guid = guid++;
			// create a hash table of event types for the element
			if (!element.events) element.events = {};
			// create a hash table of event handlers for each element/event pair
			var handlers = element.events[type];
			if (!handlers) {
				handlers = element.events[type] = {};
				// store the existing event handler (if there is one)
				if (element["on" + type]) {
					handlers[0] = element["on" + type];
				}
			}
			// store the event handler in the hash table
			handlers[handler.$$guid] = handler;
			// assign a global event handler to do all the work
			element["on" + type] = handleEvent;
		}
    }
    function _removeEvent(element, type, handler) {
        if (element.removeEventListener) {
            element.removeEventListener(type, handler, false);
        } else {
			// delete the event handler from the hash table
            if (element.events && element.events[type]) {
                delete element.events[type][handler.$$guid];
            }
        }
    }
    function handleEvent(event){
		var returnValue = true;
		// grab the event object (IE uses a global event object)
		event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
		// get a reference to the hash table of event handlers
		var handlers = this.events[event.type];
		// execute each event handler
		for (var i in handlers) {
			this.$$handleEvent = handlers[i];
			if (this.$$handleEvent(event) === false) {
				returnValue = false;
			}
		}
		return returnValue;
    }
    function fixEvent(event){
		// add W3C standard event methods
        event.preventDefault=fixEvent.preventDefault;
        event.stopPropagation=fixEvent.stopPropagation;
        return event;
    }
    fixEvent.preventDefault  = function(){this.returnValue=false;}
    fixEvent.stopPropagation = function(){this.cancelBubble=true;}
    
	//http://www.thefutureoftheweb.com/blog/adddomloadevent
	var load_events = [],isReady,script,exec,
		init = function () {
			if(isReady) return;isReady = true;
			if (document.removeEventListener){
				document.removeEventListener("DOMContentLoaded", init, false);
			}else if(document.detachEvent && document.readyState === "complete"){
				document.detachEvent("onreadystatechange", init );
			}
			if (script){
				script.onreadystatechange = '';
				script.removeNode(true);
			} 
			//execute each function in the stack in the order they were added
			while (exec = load_events.shift()) exec();
		};
    return {
        domReady:function(func) {
			if (!load_events[0]) {
				// if the init function was already ran, just run this function now and stop
				if (isReady || document.readyState === "complete" ) {
					return setTimeout( func, 1 );
				// for Mozilla/Opera9
				}else if (document.addEventListener){
					document.addEventListener("DOMContentLoaded", init, false);
				// If IE event model is used
				}else{
					var proto = "javascript:void(0)";                    
					if (location.protocol == "https:") proto = "//0";
					document.write("<script id=__ie_onload defer src="+proto+"><\/scr"+"ipt>");
					script = document.getElementById("__ie_onload");
					script.onreadystatechange = function() {
						if (this.readyState == "complete"){
							init(); 
						} 
					};
                    // If IE is used and is not in a frame
                    var top = false;
					try {
						top = (window.frameElement == null && document.documentElement);
					} catch(e) {}
                    
					if(top && top.doScroll) void function(){ 
					 	if (isReady) return;
					    try {
					    	top.doScroll("left");
					    } catch(e) { 
					    	return setTimeout(arguments.callee,32);
					    }
					    init();
					}();
					
					// for Safari
					if (/WebKit/i.test(navigator.userAgent)) { // sniff 
						if (document.readyState != "complete") {
							var load_timer = setInterval(function() {
								if (/loaded|complete/.test(document.readyState)) {
									clearInterval(load_timer);
									init(); // call the onload handler
								}
							}, 10);
						}
					}
				}
				// for other browsers set the window.onload, but also execute the old window.onload
				_addEvent(window,"load",init);
			}
			load_events.push(func);
		},
        stopPropagation:function(ev){
            var evt = ev||window.event;
            if (evt.stopPropagation){ 
            	evt.stopPropagation();// MOZ style 
            }else{
            	evt.cancelBubble = true;//IE style
            }
            return false;
        },
        preventDefault:function(ev){
           var evt = ev ||window.event;
           if (evt.preventDefault) {
		       evt.preventDefault();
		   } else {
		       evt.returnValue = false;
		   }
        },
        addEvent:_addEvent,
        removeEvent:_removeEvent
    }
})();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值