]> git.madduck.net Git - debian/web/pdo.git/blob - js/event-registration.js

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

initial commit
[debian/web/pdo.git] / js / event-registration.js
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
4 \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
10                 return;\r
11         }\r
12         \r
13         if (element.addEventListener) {\r
14                 element.addEventListener(type, handler, false);\r
15         } else {\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
22                 if (!handlers) {\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
27                         }\r
28                 }\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
33         }\r
34 };\r
35 // a counter used to create unique IDs\r
36 addEvent.guid = 1;\r
37 \r
38 function removeEvent(element, type, handler) {\r
39         if (element.removeEventListener) {\r
40                 element.removeEventListener(type, handler, false);\r
41         } else {\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
45                 }\r
46         }\r
47 };\r
48 \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
60                 }\r
61         }\r
62         return returnValue;\r
63 };\r
64 \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
69         return event;\r
70 };\r
71 fixEvent.preventDefault = function() {\r
72         this.returnValue = false;\r
73 };\r
74 fixEvent.stopPropagation = function() {\r
75         this.cancelBubble = true;\r
76 };\r
77 \r
78 // End Dean Edwards addEvent.\r
79 \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
87                 }\r
88         }\r
89 }\r
90 \r
91 // Here are my functions for adding the DOMContentLoaded event to browsers other\r
92 // than Mozilla.\r
93 \r
94 // Array of DOMContentLoaded event handlers.\r
95 window.onDOMLoadEvents = new Array();\r
96 window.DOMContentLoadedInitDone = false;\r
97 \r
98 // Function that adds DOMContentLoaded listeners to the array.\r
99 function addDOMLoadEvent(listener) {\r
100         window.onDOMLoadEvents[window.onDOMLoadEvents.length]=listener;\r
101 }\r
102 \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
107 \r
108         // flag this function so we don't do the same thing twice\r
109         window.DOMContentLoadedInitDone = true;\r
110 \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
114                 func();\r
115         }\r
116 }\r
117 \r
118 function DOMContentLoadedScheduler() {\r
119         // quit if the init function has already been called\r
120         if (window.DOMContentLoadedInitDone) return true;\r
121         \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
130                 } else {\r
131                         // Not ready yet, wait a little more.\r
132                         setTimeout("DOMContentLoadedScheduler()", 250);\r
133                 }\r
134         } else if(document.getElementById("__ie_onload")) {\r
135                 return true;\r
136         } else if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {\r
137                 DOMContentLoadedInit();\r
138         } else {\r
139                 // Not ready yet, wait a little more.\r
140                 setTimeout("DOMContentLoadedScheduler()", 250);\r
141         }\r
142         \r
143         return true;\r
144 }\r
145 \r
146 // Schedule to run the init function.\r
147 setTimeout("DOMContentLoadedScheduler()", 250);\r
148 \r
149 // Just in case window.onload happens first, add it there too.\r
150 addEvent(window, "load", DOMContentLoadedInit);\r
151 \r
152 // If addEventListener supports the DOMContentLoaded event.\r
153 if(document.addEventListener)\r
154         document.addEventListener("DOMContentLoaded", DOMContentLoadedInit, false);\r
155 \r
156 /* for Internet Explorer */\r
157 /*@cc_on @*/\r
158 /*@if (@_win32)\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
164                 }\r
165         };\r
166 /*@end @*/\r