1
// written by Dean Edwards, 2005
2
// with input from Tino Zijdel
4
// http://dean.edwards.name/weblog/2005/10/add-event/
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];
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];
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;
25
// a counter used to create unique IDs
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];
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) {
51
function fixEvent(event) {
52
// add W3C standard event methods
53
event.preventDefault = fixEvent.preventDefault;
54
event.stopPropagation = fixEvent.stopPropagation;
56
event.target = event.srcElement;
59
fixEvent.preventDefault = function() {
60
this.returnValue = false;
62
fixEvent.stopPropagation = function() {
63
this.cancelBubble = true;
68
* Pseudo event handler to be fired after the DOM was parsed or
69
* on window load at last.
71
* @author based upon some code by Dean Edwards
72
* @author Dean Edwards
73
* @link http://dean.edwards.name/weblog/2006/06/again/
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;
82
clearInterval(_timer);
86
if (typeof window.oninit == 'function') {
92
* Run the fireoninit function as soon as possible after
93
* the DOM was loaded, using different methods for different
96
* @author Dean Edwards
97
* @link http://dean.edwards.name/weblog/2006/06/again/
100
if (document.addEventListener) {
101
document.addEventListener("DOMContentLoaded", window.fireoninit, null);
104
// for Internet Explorer (using conditional comments)
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
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
125
// for other browsers
126
window.onload = window.fireoninit;
130
* This is a pseudo Event that will be fired by the fireoninit
133
* Use addInitEvent to bind to this event!
135
* @author Andreas Gohr <andi@splitbrain.org>
138
window.oninit = function(){};
141
* Bind a function to the window.init pseudo event
143
* @author Simon Willison
144
* @see http://simon.incutio.com/archive/2004/05/26/addLoadEvent
146
function addInitEvent(func) {
147
var oldoninit = window.oninit;
148
if (typeof window.oninit != 'function') {
149
window.oninit = func;
151
window.oninit = function() {