var K = function () { 
    var undef; 
    return { 
        module : function (newmodule) { 
            if (this[newmodule] === undef) { 
                this[newmodule] = {}; 
                return this[newmodule]; 
            } 
            return this[newmodule]; 
        } 
    }; 
}() ; K.module('util'); K.util.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 = [],load_timer,script,done,exec, 
        init = function () { 
            done = true; 
            clearInterval(load_timer);// kill the timer 
            // execute each function in the stack in the order they were added 
            while (exec = load_events.shift()) exec(); 
            if (script) script.onreadystatechange = ''; 
        }; 
    return { 
        domReady:function(func) { 
            // if the init function was already ran, just run this function now and stop 
            if (done) return func(); 
            if (!load_events[0]) { 
                // for Mozilla/Opera9 
                if (document.addEventListener) document.addEventListener("DOMContentLoaded", init, false);                 // for Internet Explorer 
                /*@cc_on @*/ 
                /*@if (@_win32) 
                    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();  
                    }; 
                /*@end @*/                 // for Safari 
                if (/WebKit/i.test(navigator.userAgent)) { // sniff 
                    load_timer = setInterval(function() { 
                        if (/loaded|complete/.test(document.readyState)) 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); 
        }, 
        addEvent:_addEvent, 
        removeEvent:_removeEvent 
    } 
})(); 
K.util.event.domReady(function(){ 
    alert("dom ready!") 
})下载地址:http://kingfishers.googlecode.com/files/K.util.event.js

解决方案 »

  1.   

    //2010-08-30 21:30 update// for Internet Explorer  
    /*@cc_on @*/  
    /*@if (@_win32)  
        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(); 
    }   
        };  
        //2010-08-30 21:30 update
        if(window == top) new function(){ 
    try{ 
        document.documentElement.doScroll(); 
    }catch(e){ 
        setTimeout(arguments.callee, 32);  
        return; 

    init(); 
        }(); 
    /*@end @*/