[ Index ] |
|
Code source de DokuWiki 2006-11-06 |
1 // written by Dean Edwards, 2005 2 // with input from Tino Zijdel 3 4 // http://dean.edwards.name/weblog/2005/10/add-event/ 5 6 function addEvent(element, type, handler) { 7 // assign each event handler a unique ID 8 if (!handler.$$guid) handler.$$guid = addEvent.guid++; 9 // create a hash table of event types for the element 10 if (!element.events) element.events = {}; 11 // create a hash table of event handlers for each element/event pair 12 var handlers = element.events[type]; 13 if (!handlers) { 14 handlers = element.events[type] = {}; 15 // store the existing event handler (if there is one) 16 if (element["on" + type]) { 17 handlers[0] = element["on" + type]; 18 } 19 } 20 // store the event handler in the hash table 21 handlers[handler.$$guid] = handler; 22 // assign a global event handler to do all the work 23 element["on" + type] = handleEvent; 24 }; 25 // a counter used to create unique IDs 26 addEvent.guid = 1; 27 28 function removeEvent(element, type, handler) { 29 // delete the event handler from the hash table 30 if (element.events && element.events[type]) { 31 delete element.events[type][handler.$$guid]; 32 } 33 }; 34 35 function handleEvent(event) { 36 var returnValue = true; 37 // grab the event object (IE uses a global event object) 38 event = event || fixEvent(window.event); 39 // get a reference to the hash table of event handlers 40 var handlers = this.events[event.type]; 41 // execute each event handler 42 for (var i in handlers) { 43 this.$$handleEvent = handlers[i]; 44 if (this.$$handleEvent(event) === false) { 45 returnValue = false; 46 } 47 } 48 return returnValue; 49 }; 50 51 function fixEvent(event) { 52 // add W3C standard event methods 53 event.preventDefault = fixEvent.preventDefault; 54 event.stopPropagation = fixEvent.stopPropagation; 55 // fix target 56 event.target = event.srcElement; 57 return event; 58 }; 59 fixEvent.preventDefault = function() { 60 this.returnValue = false; 61 }; 62 fixEvent.stopPropagation = function() { 63 this.cancelBubble = true; 64 }; 65 66 67 /** 68 * Pseudo event handler to be fired after the DOM was parsed or 69 * on window load at last. 70 * 71 * @author based upon some code by Dean Edwards 72 * @author Dean Edwards 73 * @link http://dean.edwards.name/weblog/2006/06/again/ 74 */ 75 window.fireoninit = function() { 76 // quit if this function has already been called 77 if (arguments.callee.done) return; 78 // flag this function so we don't do the same thing twice 79 arguments.callee.done = true; 80 // kill the timer 81 if (_timer) { 82 clearInterval(_timer); 83 _timer = null; 84 } 85 86 if (typeof window.oninit == 'function') { 87 window.oninit(); 88 } 89 }; 90 91 /** 92 * Run the fireoninit function as soon as possible after 93 * the DOM was loaded, using different methods for different 94 * Browsers 95 * 96 * @author Dean Edwards 97 * @link http://dean.edwards.name/weblog/2006/06/again/ 98 */ 99 // for Mozilla 100 if (document.addEventListener) { 101 document.addEventListener("DOMContentLoaded", window.fireoninit, null); 102 } 103 104 // for Internet Explorer (using conditional comments) 105 /*@cc_on @*/ 106 /*@if (@_win32) 107 document.write("<scr" + "ipt id=\"__ie_init\" defer=\"true\" src=\"//:\"><\/script>"); 108 var script = document.getElementById("__ie_init"); 109 script.onreadystatechange = function() { 110 if (this.readyState == "complete") { 111 window.fireoninit(); // call the onload handler 112 } 113 }; 114 /*@end @*/ 115 116 // for Safari 117 if (/WebKit/i.test(navigator.userAgent)) { // sniff 118 var _timer = setInterval(function() { 119 if (/loaded|complete/.test(document.readyState)) { 120 window.fireoninit(); // call the onload handler 121 } 122 }, 10); 123 } 124 125 // for other browsers 126 window.onload = window.fireoninit; 127 128 129 /** 130 * This is a pseudo Event that will be fired by the fireoninit 131 * function above. 132 * 133 * Use addInitEvent to bind to this event! 134 * 135 * @author Andreas Gohr <andi@splitbrain.org> 136 * @see fireoninit() 137 */ 138 window.oninit = function(){}; 139 140 /** 141 * Bind a function to the window.init pseudo event 142 * 143 * @author Simon Willison 144 * @see http://simon.incutio.com/archive/2004/05/26/addLoadEvent 145 */ 146 function addInitEvent(func) { 147 var oldoninit = window.oninit; 148 if (typeof window.oninit != 'function') { 149 window.oninit = func; 150 } else { 151 window.oninit = function() { 152 oldoninit(); 153 func(); 154 }; 155 } 156 } 157 158
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Tue Apr 3 20:47:31 2007 | par Balluche grâce à PHPXref 0.7 |