I've completely resolved the known issues with document ready. I use the known DOM...
[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 // Handle when the DOM is ready
45 $.ready = function(isFinal) {
46         // If the timer was running, stop it
47         if ( $.$$timer ) {
48                 clearInterval( $.$$timer );
49                 $.$$timer = null;
50         }
51
52         // If the last script to fire was in the body,
53         // we assume that it's trying to do a document.write
54         var s = document.getElementsByTagName("script");
55         s = s[s.length-1].parentNode.nodeName == "HEAD";
56
57         // Only execute if we're doing a sane way, or the window
58         // is loaded, or the final script is in the head
59         // and there's something to execute
60         if ( ( !$.badReady || isFinal || s ) && $.$$ready ) {
61                 for ( var i = 0; i < $.$$ready.length; i++ ) {
62                         $.apply( document, $.$$ready[i] );
63                 }
64                 $.$$ready = null;
65         }
66 };
67
68 // Based off of:
69 // http://linguiste.org/projects/behaviour-DOMContentLoaded/example.html
70
71 // If Mozilla is used
72 if ( $.browser == "mozilla" ) {
73         // Use the handy event callback
74         document.addEventListener( "DOMContentLoaded", $.ready, null );
75
76 // If IE is used
77 } else if ( $.browser == "msie" ) {
78         // Use the defer script hack
79         var script = document.createElement('SCRIPT');
80         script.type = 'text/javascript';
81         script.src = 'javascript:$.ready();void(0);';
82         script.defer = true;
83         document.getElementsByTagName('HEAD')[0].appendChild(script);
84         script = null;
85
86 // Otherwise, try it the hacky way
87 } else {
88         $.badReady = true;
89 }
90
91 // A fallback, that will always work, just in case
92 $.event.add( window, "load", function(){
93         $.ready(true);
94 });
95
96 /**
97  * Bind a function to fire when the DOM is ready.
98  */
99 $.fn.ready = function(f) {
100         return this.each(function(){
101                 if ( $.$$ready ) {
102                         $.$$ready.push( f );
103                 } else {
104                         var o = this;
105                         $.$$ready = [ f ];
106
107                         // Only do our hacky thing if we don't have the nice
108                         // Mozilla or IE ways of doing it       
109                         if ( $.$$badReady ) {
110                                 // The trick is to check for the availability of a couple common
111                                 // DOM functions, if they exist, assume the DOM is ready
112                                 $.$$timer = setInterval( function(){
113                                         if ( o && o.getElementsByTagName && o.getElementById && o.body ) {
114                                                 $.ready();
115                                         }
116                                 }, 10 );
117                         }
118                 }
119         });
120 };
121
122 $.fn.toggle = function(a,b) {
123         return a && b ? this.click(function(e){
124                 this.$$last = this.$$last == a ? b : a;
125                 e.preventDefault();
126                 return $.apply( this, this.$$last, [e] ) || false;
127         }) : this._toggle();
128 };