All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 // written by Dean Edwards, 2005
\r
2 // with input from Tino Zijdel, Matthias Miller, Diego Perini
\r
3 // http://dean.edwards.name/weblog/2005/10/add-event/
\r
5 function addEvent(element, type, handler) {
\r
6 // Modification by Tanny O'Haley, http://tanny.ica.com to add the
\r
7 // DOMContentLoaded for all browsers.
\r
8 if (type == "DOMContentLoaded" || type == "domload") {
\r
9 addDOMLoadEvent(handler);
\r
13 if (element.addEventListener) {
\r
14 element.addEventListener(type, handler, false);
\r
16 // assign each event handler a unique ID
\r
17 if (!handler.$$guid) handler.$$guid = addEvent.guid++;
\r
18 // create a hash table of event types for the element
\r
19 if (!element.events) element.events = {};
\r
20 // create a hash table of event handlers for each element/event pair
\r
21 var handlers = element.events[type];
\r
23 handlers = element.events[type] = {};
\r
24 // store the existing event handler (if there is one)
\r
25 if (element["on" + type]) {
\r
26 handlers[0] = element["on" + type];
\r
29 // store the event handler in the hash table
\r
30 handlers[handler.$$guid] = handler;
\r
31 // assign a global event handler to do all the work
\r
32 element["on" + type] = handleEvent;
\r
35 // a counter used to create unique IDs
\r
38 function removeEvent(element, type, handler) {
\r
39 if (element.removeEventListener) {
\r
40 element.removeEventListener(type, handler, false);
\r
42 // delete the event handler from the hash table
\r
43 if (element.events && element.events[type]) {
\r
44 delete element.events[type][handler.$$guid];
\r
49 function handleEvent(event) {
\r
50 var returnValue = true;
\r
51 // grab the event object (IE uses a global event object)
\r
52 event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
\r
53 // get a reference to the hash table of event handlers
\r
54 var handlers = this.events[event.type];
\r
55 // execute each event handler
\r
56 for (var i in handlers) {
\r
57 this.$$handleEvent = handlers[i];
\r
58 if (this.$$handleEvent(event) === false) {
\r
59 returnValue = false;
\r
65 function fixEvent(event) {
\r
66 // add W3C standard event methods
\r
67 event.preventDefault = fixEvent.preventDefault;
\r
68 event.stopPropagation = fixEvent.stopPropagation;
\r
71 fixEvent.preventDefault = function() {
\r
72 this.returnValue = false;
\r
74 fixEvent.stopPropagation = function() {
\r
75 this.cancelBubble = true;
\r
78 // End Dean Edwards addEvent.
\r
80 // Tino Zijdel - crisp@xs4all.nl This little snippet fixes the problem that the onload attribute on
\r
81 // the body-element will overwrite previous attached events on the window object for the onload event.
\r
82 if (!window.addEventListener) {
\r
83 document.onreadystatechange = function(){
\r
84 if (window.onload && window.onload != handleEvent) {
\r
85 addEvent(window, 'load', window.onload);
\r
86 window.onload = handleEvent;
\r
91 // Here are my functions for adding the DOMContentLoaded event to browsers other
\r
94 // Array of DOMContentLoaded event handlers.
\r
95 window.onDOMLoadEvents = new Array();
\r
96 window.DOMContentLoadedInitDone = false;
\r
98 // Function that adds DOMContentLoaded listeners to the array.
\r
99 function addDOMLoadEvent(listener) {
\r
100 window.onDOMLoadEvents[window.onDOMLoadEvents.length]=listener;
\r
103 // Function to process the DOMContentLoaded events array.
\r
104 function DOMContentLoadedInit() {
\r
105 // quit if this function has already been called
\r
106 if (window.DOMContentLoadedInitDone) return;
\r
108 // flag this function so we don't do the same thing twice
\r
109 window.DOMContentLoadedInitDone = true;
\r
111 // iterates through array of registered functions
\r
112 for (var i=0; i<window.onDOMLoadEvents.length; i++) {
\r
113 var func = window.onDOMLoadEvents[i];
\r
118 function DOMContentLoadedScheduler() {
\r
119 // quit if the init function has already been called
\r
120 if (window.DOMContentLoadedInitDone) return true;
\r
122 // First, check for Safari or KHTML.
\r
123 // Second, check for IE.
\r
124 //if DOM methods are supported, and the body element exists
\r
125 //(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1]
\r
126 //in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section)
\r
127 if(/KHTML|WebKit/i.test(navigator.userAgent)) {
\r
128 if(/loaded|complete/.test(document.readyState)) {
\r
129 DOMContentLoadedInit();
\r
131 // Not ready yet, wait a little more.
\r
132 setTimeout("DOMContentLoadedScheduler()", 250);
\r
134 } else if(document.getElementById("__ie_onload")) {
\r
136 } else if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {
\r
137 DOMContentLoadedInit();
\r
139 // Not ready yet, wait a little more.
\r
140 setTimeout("DOMContentLoadedScheduler()", 250);
\r
146 // Schedule to run the init function.
\r
147 setTimeout("DOMContentLoadedScheduler()", 250);
\r
149 // Just in case window.onload happens first, add it there too.
\r
150 addEvent(window, "load", DOMContentLoadedInit);
\r
152 // If addEventListener supports the DOMContentLoaded event.
\r
153 if(document.addEventListener)
\r
154 document.addEventListener("DOMContentLoaded", DOMContentLoadedInit, false);
\r
156 /* for Internet Explorer */
\r
159 document.write("<script id=__ie_onload defer src=\"//:\"><\/script>");
\r
160 var script = document.getElementById("__ie_onload");
\r
161 script.onreadystatechange = function() {
\r
162 if (this.readyState == "complete") {
\r
163 DOMContentLoadedInit(); // call the onload handler
\r