Fixed the double-firing IE bug. (Ticket #13)
[jquery.git] / event / event.js
1 (function(){
2         var e = ["blur","focus","contextmenu","load","resize","scroll","unload",
3                 "click","dblclick","mousedown","mouseup","mouseenter","mouseleave",
4                 "mousemove","mouseover","mouseout","change","reset","select","submit",
5                 "keydown","keypress","keyup","abort","error","ready"];
6
7         for ( var i = 0; i < e.length; i++ ) {
8                 (function(){
9                         var o = e[i];
10                         $.fn[o] = function(f){ return this.bind(o, f); };
11                         $.fn["un"+o] = function(f){ return this.unbind(o, f); };
12                         $.fn["do"+o] = function(){ return this.trigger(o); };
13                         $.fn["one"+o] = function(f){ return this.bind(o, function(e){
14                                 if ( this[o+f] !== null ) { return true; }
15                                 this[o+f]++;
16                                 return $.apply(this,f,[e]);
17                         }); };
18                 
19                         // Deprecated
20                         //$.fn["on"+o] = function(f){ return this.bind(o, f); };
21                 })();
22         }
23 })();
24
25 $.fn.hover = function(f,g) {
26         // Check if mouse(over|out) are still within the same parent element
27         return this.each(function(){
28                 var obj = this;
29                 $.event.add(this, "mouseover", function(e) {
30                         var p = ( e.fromElement !== null ? e.fromElement : e.relatedTarget );
31                         while ( p && p != obj ) { p = p.parentNode; }
32                         if ( p == obj ) { return false; }
33                         return $.apply(obj,f,[e]);
34                 });
35                 $.event.add(this, "mouseout", function(e) {
36                         var p = ( e.toElement !== null ? e.toElement : e.relatedTarget );
37                         while ( p && p != obj ) { p = p.parentNode; }
38                         if ( p == obj ) { return false; }
39                         return $.apply(obj,g,[e]);
40                 });
41         });
42 };
43
44 $.$$isReady = false;
45 $.$$ready = [];
46
47 // Handle when the DOM is ready
48 $.ready = function() {
49         if ( !$.$$isReady ) {
50                 $.$$isReady = true;
51                 if ( $.$$ready ) {
52                         for ( var i = 0; i < $.$$ready.length; i++ ) {
53                                 $.apply( document, $.$$ready[i] );
54                         }
55                         $.$$ready = [];
56                 }
57         }
58 };
59
60 // If Mozilla is used
61 if ( $.browser == "mozilla" || $.browser == "opera" ) {
62         // Use the handy event callback
63         document.addEventListener( "DOMContentLoaded", $.ready, null );
64
65 // If IE is used, use the excellent hack by Matthias Miller
66 // http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
67 } else if ( $.browser == "msie" ) {
68
69         // Only works if you document.write() it
70         document.write('<scr' + 'ipt id=__ie_init defer=true ' + 
71                 'src=javascript:void(0)><\/script>');
72
73         // Use the defer script hack
74         var script = document.getElementById('__ie_init');
75         script.onreadystatechange = function() {
76                 if ( this.readyState == 'complete' ) {
77                         $.ready();
78                 }
79         };
80
81         // Clear from memory
82         script = null;
83
84 // If Safari  is used
85 } else if ( $.browser == "safari" ) {
86         $.$$timer = setInterval(function(){
87         if ( document.readyState == "loaded" || 
88                         document.readyState == "complete" ) {
89
90                         clearInterval( $.$$timer );
91                         $.$$timer = null;
92
93                         $.ready();
94                 }
95         }, 10);
96 }
97
98 // A fallback, that will always work, just in case
99 $.event.add( window, "load", $.ready );
100
101 /**
102  * Bind a function to fire when the DOM is ready.
103  */
104 $.fn.ready = function(f) {
105         if ( $.$$isReady ) {
106                 $.apply( document, f );
107         } else {
108                 if ( ! $.$$ready ) {
109                         $.$$ready = [];
110                 }
111
112                 $.$$ready.push( f );
113         }
114
115         return this;
116 };
117
118 $.fn.toggle = function(a,b) {
119         return a && b ? this.click(function(e){
120                 this.$$last = this.$$last == a ? b : a;
121                 e.preventDefault();
122                 return $.apply( this, this.$$last, [e] ) || false;
123         }) : this._toggle();
124 };