1 // written by Dean Edwards, 2005
2 // with input from Tino Zijdel, Matthias Miller, Diego Perini
3 // http://dean.edwards.name/weblog/2005/10/add-event/
4 function addEvent(element, type, handler) {
5 // Modification by Tanny O'Haley, http://tanny.ica.com to add the
6 // DOMContentLoaded for all browsers.
7 if (type == "DOMContentLoaded" || type == "domload") {
8 addDOMLoadEvent(handler);
12 if (element.addEventListener) {
13 element.addEventListener(type, handler, false);
15 // assign each event handler a unique ID
16 if (!handler.$$guid) handler.$$guid = addEvent.guid++;
17 // create a hash table of event types for the element
18 if (!element.events) element.events = {};
19 // create a hash table of event handlers for each element/event pair
20 var handlers = element.events[type];
22 handlers = element.events[type] = {};
23 // store the existing event handler (if there is one)
24 if (element["on" + type]) {
25 handlers[0] = element["on" + type];
28 // store the event handler in the hash table
29 handlers[handler.$$guid] = handler;
30 // assign a global event handler to do all the work
31 element["on" + type] = handleEvent;
34 // a counter used to create unique IDs
37 function removeEvent(element, type, handler) {
38 if (element.removeEventListener) {
39 element.removeEventListener(type, handler, false);
41 // delete the event handler from the hash table
42 if (element.events && element.events[type]) {
43 delete element.events[type][handler.$$guid];
48 function handleEvent(event) {
49 var returnValue = true;
50 // grab the event object (IE uses a global event object)
51 event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
52 // get a reference to the hash table of event handlers
53 var handlers = this.events[event.type];
54 // execute each event handler
55 for (var i in handlers) {
56 this.$$handleEvent = handlers[i];
57 if (this.$$handleEvent(event) === false) {
64 function fixEvent(event) {
65 // add W3C standard event methods
66 event.preventDefault = fixEvent.preventDefault;
67 event.stopPropagation = fixEvent.stopPropagation;
70 fixEvent.preventDefault = function() {
71 this.returnValue = false;
73 fixEvent.stopPropagation = function() {
74 this.cancelBubble = true;
77 // End Dean Edwards addEvent.
79 // Tino Zijdel - crisp@xs4all.nl This little snippet fixes the problem that the onload attribute on
80 // the body-element will overwrite previous attached events on the window object for the onload event.
81 if (!window.addEventListener) {
82 document.onreadystatechange = function(){
83 if (window.onload && window.onload != handleEvent) {
84 addEvent(window, 'load', window.onload);
85 window.onload = handleEvent;
90 // Here are my functions for adding the DOMContentLoaded event to browsers other
93 // Array of DOMContentLoaded event handlers.
94 window.onDOMLoadEvents = new Array();
95 window.DOMContentLoadedInitDone = false;
97 // Function that adds DOMContentLoaded listeners to the array.
98 function addDOMLoadEvent(listener) {
99 window.onDOMLoadEvents[window.onDOMLoadEvents.length]=listener;
102 // Function to process the DOMContentLoaded events array.
103 function DOMContentLoadedInit() {
104 // quit if this function has already been called
105 if (window.DOMContentLoadedInitDone) return;
107 // flag this function so we don't do the same thing twice
108 window.DOMContentLoadedInitDone = true;
110 // iterates through array of registered functions
111 for (var i=0; i<window.onDOMLoadEvents.length; i++) {
112 var func = window.onDOMLoadEvents[i];
117 function DOMContentLoadedScheduler() {
118 // quit if the init function has already been called
119 if (window.DOMContentLoadedInitDone) return true;
121 // First, check for Safari or KHTML.
122 // Second, check for IE.
123 //if DOM methods are supported, and the body element exists
124 //(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1]
125 //in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section)
126 if(/KHTML|WebKit/i.test(navigator.userAgent)) {
127 if(/loaded|complete/.test(document.readyState)) {
128 DOMContentLoadedInit();
130 // Not ready yet, wait a little more.
131 setTimeout("DOMContentLoadedScheduler()", 250);
133 } else if(document.getElementById("__ie_onload")) {
135 } else if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {
136 DOMContentLoadedInit();
138 // Not ready yet, wait a little more.
139 setTimeout("DOMContentLoadedScheduler()", 250);
145 // Schedule to run the init function.
146 setTimeout("DOMContentLoadedScheduler()", 250);
148 // Just in case window.onload happens first, add it there too.
149 addEvent(window, "load", DOMContentLoadedInit);
151 // If addEventListener supports the DOMContentLoaded event.
152 if(document.addEventListener)
153 document.addEventListener("DOMContentLoaded", DOMContentLoadedInit, false);
155 /* for Internet Explorer */
158 document.write("<script id=__ie_onload defer src=\"//:\"><\/script>");
159 var script = document.getElementById("__ie_onload");
160 script.onreadystatechange = function() {
161 if (this.readyState == "complete") {
162 DOMContentLoadedInit(); // call the onload handler