Backported wiki changes from http://docs.jquery.com/API/1.1.1/Events
[jquery.git] / src / event / event.js
1 /*
2  * A number of helper functions used for managing events.
3  * Many of the ideas behind this code orignated from 
4  * Dean Edwards' addEvent library.
5  */
6 jQuery.event = {
7
8         // Bind an event to an element
9         // Original by Dean Edwards
10         add: function(element, type, handler, data) {
11                 // For whatever reason, IE has trouble passing the window object
12                 // around, causing it to be cloned in the process
13                 if ( jQuery.browser.msie && element.setInterval != undefined )
14                         element = window;
15
16                 // if data is passed, bind to handler
17                 if( data ) 
18                         handler.data = data;
19
20                 // Make sure that the function being executed has a unique ID
21                 if ( !handler.guid )
22                         handler.guid = this.guid++;
23
24                 // Init the element's event structure
25                 if (!element.$events)
26                         element.$events = {};
27
28                 // Get the current list of functions bound to this event
29                 var handlers = element.$events[type];
30
31                 // If it hasn't been initialized yet
32                 if (!handlers) {
33                         // Init the event handler queue
34                         handlers = element.$events[type] = {};
35
36                         // Remember an existing handler, if it's already there
37                         if (element["on" + type])
38                                 handlers[0] = element["on" + type];
39                 }
40
41                 // Add the function to the element's handler list
42                 handlers[handler.guid] = handler;
43
44                 // And bind the global event handler to the element
45                 element["on" + type] = this.handle;
46
47                 // Remember the function in a global list (for triggering)
48                 if (!this.global[type])
49                         this.global[type] = [];
50                 this.global[type].push( element );
51         },
52
53         guid: 1,
54         global: {},
55
56         // Detach an event or set of events from an element
57         remove: function(element, type, handler) {
58                 if (element.$events) {
59                         var i,j,k;
60                         if ( type && type.type ) { // type is actually an event object here
61                                 handler = type.handler;
62                                 type    = type.type;
63                         }
64                         
65                         if (type && element.$events[type])
66                                 // remove the given handler for the given type
67                                 if ( handler )
68                                         delete element.$events[type][handler.guid];
69                                         
70                                 // remove all handlers for the given type
71                                 else
72                                         for ( i in element.$events[type] )
73                                                 delete element.$events[type][i];
74                                                 
75                         // remove all handlers          
76                         else
77                                 for ( j in element.$events )
78                                         this.remove( element, j );
79                         
80                         // remove event handler if no more handlers exist
81                         for ( k in element.$events[type] )
82                                 if (k) {
83                                         k = true;
84                                         break;
85                                 }
86                         if (!k) element["on" + type] = null;
87                 }
88         },
89
90         trigger: function(type, data, element) {
91                 // Clone the incoming data, if any
92                 data = jQuery.makeArray(data || []);
93
94                 // Handle a global trigger
95                 if ( !element )
96                         jQuery.each( this.global[type] || [], function(){
97                                 jQuery.event.trigger( type, data, this );
98                         });
99
100                 // Handle triggering a single element
101                 else {
102                         var handler = element["on" + type ], val,
103                                 fn = jQuery.isFunction( element[ type ] );
104
105                         if ( handler ) {
106                                 // Pass along a fake event
107                                 data.unshift( this.fix({ type: type, target: element }) );
108         
109                                 // Trigger the event
110                                 if ( (val = handler.apply( element, data )) !== false )
111                                         this.triggered = true;
112                         }
113
114                         if ( fn && val !== false )
115                                 element[ type ]();
116
117                         this.triggered = false;
118                 }
119         },
120
121         handle: function(event) {
122                 // Handle the second event of a trigger and when
123                 // an event is called after a page has unloaded
124                 if ( typeof jQuery == "undefined" || jQuery.event.triggered ) return;
125
126                 // Empty object is for triggered events with no data
127                 event = jQuery.event.fix( event || window.event || {} ); 
128
129                 // returned undefined or false
130                 var returnValue;
131
132                 var c = this.$events[event.type];
133
134                 var args = [].slice.call( arguments, 1 );
135                 args.unshift( event );
136
137                 for ( var j in c ) {
138                         // Pass in a reference to the handler function itself
139                         // So that we can later remove it
140                         args[0].handler = c[j];
141                         args[0].data = c[j].data;
142
143                         if ( c[j].apply( this, args ) === false ) {
144                                 event.preventDefault();
145                                 event.stopPropagation();
146                                 returnValue = false;
147                         }
148                 }
149
150                 // Clean up added properties in IE to prevent memory leak
151                 if (jQuery.browser.msie) event.target = event.preventDefault = event.stopPropagation = event.handler = event.data = null;
152
153                 return returnValue;
154         },
155
156         fix: function(event) {
157                 // Fix target property, if necessary
158                 if ( !event.target && event.srcElement )
159                         event.target = event.srcElement;
160
161                 // Calculate pageX/Y if missing and clientX/Y available
162                 if ( event.pageX == undefined && event.clientX != undefined ) {
163                         var e = document.documentElement, b = document.body;
164                         event.pageX = event.clientX + (e.scrollLeft || b.scrollLeft);
165                         event.pageY = event.clientY + (e.scrollTop || b.scrollTop);
166                 }
167                                 
168                 // check if target is a textnode (safari)
169                 if (jQuery.browser.safari && event.target.nodeType == 3) {
170                         // store a copy of the original event object 
171                         // and clone because target is read only
172                         var originalEvent = event;
173                         event = jQuery.extend({}, originalEvent);
174                         
175                         // get parentnode from textnode
176                         event.target = originalEvent.target.parentNode;
177                         
178                         // add preventDefault and stopPropagation since 
179                         // they will not work on the clone
180                         event.preventDefault = function() {
181                                 return originalEvent.preventDefault();
182                         };
183                         event.stopPropagation = function() {
184                                 return originalEvent.stopPropagation();
185                         };
186                 }
187                 
188                 // fix preventDefault and stopPropagation
189                 if (!event.preventDefault)
190                         event.preventDefault = function() {
191                                 this.returnValue = false;
192                         };
193                         
194                 if (!event.stopPropagation)
195                         event.stopPropagation = function() {
196                                 this.cancelBubble = true;
197                         };
198                         
199                 return event;
200         }
201 };
202
203 jQuery.fn.extend({
204
205         /**
206          * Binds a handler to a particular event (like click) for each matched element.
207          * The event handler is passed an event object that you can use to prevent
208          * default behaviour. To stop both default action and event bubbling, your handler
209          * has to return false.
210          *
211          * In most cases, you can define your event handlers as anonymous functions
212          * (see first example). In cases where that is not possible, you can pass additional
213          * data as the second parameter (and the handler function as the third), see 
214          * second example.
215          *
216          * @example $("p").bind("click", function(){
217          *   alert( $(this).text() );
218          * });
219          * @before <p>Hello</p>
220          * @result alert("Hello")
221          *
222          * @example function handler(event) {
223          *   alert(event.data.foo);
224          * }
225          * $("p").bind("click", {foo: "bar"}, handler)
226          * @result alert("bar")
227          * @desc Pass some additional data to the event handler.
228          *
229          * @example $("form").bind("submit", function() { return false; })
230          * @desc Cancel a default action and prevent it from bubbling by returning false
231          * from your function.
232          *
233          * @example $("form").bind("submit", function(event){
234          *   event.preventDefault();
235          * });
236          * @desc Cancel only the default action by using the preventDefault method.
237          *
238          *
239          * @example $("form").bind("submit", function(event){
240          *   event.stopPropagation();
241          * });
242          * @desc Stop only an event from bubbling by using the stopPropagation method.
243          *
244          * @name bind
245          * @type jQuery
246          * @param String type An event type
247          * @param Object data (optional) Additional data passed to the event handler as event.data
248          * @param Function fn A function to bind to the event on each of the set of matched elements
249          * @cat Events
250          */
251         bind: function( type, data, fn ) {
252                 return this.each(function(){
253                         jQuery.event.add( this, type, fn || data, data );
254                 });
255         },
256         
257         /**
258          * Binds a handler to a particular event (like click) for each matched element.
259          * The handler is executed only once for each element. Otherwise, the same rules
260          * as described in bind() apply.
261          The event handler is passed an event object that you can use to prevent
262          * default behaviour. To stop both default action and event bubbling, your handler
263          * has to return false.
264          *
265          * In most cases, you can define your event handlers as anonymous functions
266          * (see first example). In cases where that is not possible, you can pass additional
267          * data as the second paramter (and the handler function as the third), see 
268          * second example.
269          *
270          * @example $("p").one("click", function(){
271          *   alert( $(this).text() );
272          * });
273          * @before <p>Hello</p>
274          * @result alert("Hello")
275          *
276          * @name one
277          * @type jQuery
278          * @param String type An event type
279          * @param Object data (optional) Additional data passed to the event handler as event.data
280          * @param Function fn A function to bind to the event on each of the set of matched elements
281          * @cat Events
282          */
283         one: function( type, data, fn ) {
284                 return this.each(function(){
285                         jQuery.event.add( this, type, function(event) {
286                                 jQuery(this).unbind(event);
287                                 return (fn || data).apply( this, arguments);
288                         }, data);
289                 });
290         },
291
292         /**
293          * The opposite of bind, removes a bound event from each of the matched
294          * elements.
295          *
296          * Without any arguments, all bound events are removed.
297          *
298          * If the type is provided, all bound events of that type are removed.
299          *
300          * If the function that was passed to bind is provided as the second argument,
301          * only that specific event handler is removed.
302          *
303          * @example $("p").unbind()
304          * @before <p onclick="alert('Hello');">Hello</p>
305          * @result [ <p>Hello</p> ]
306          *
307          * @example $("p").unbind( "click" )
308          * @before <p onclick="alert('Hello');">Hello</p>
309          * @result [ <p>Hello</p> ]
310          *
311          * @example $("p").unbind( "click", function() { alert("Hello"); } )
312          * @before <p onclick="alert('Hello');">Hello</p>
313          * @result [ <p>Hello</p> ]
314          *
315          * @name unbind
316          * @type jQuery
317          * @param String type (optional) An event type
318          * @param Function fn (optional) A function to unbind from the event on each of the set of matched elements
319          * @cat Events
320          */
321         unbind: function( type, fn ) {
322                 return this.each(function(){
323                         jQuery.event.remove( this, type, fn );
324                 });
325         },
326
327         /**
328          * Trigger a type of event on every matched element. This will also cause
329          * the default action of the browser with the same name (if one exists)
330          * to be executed. For example, passing 'submit' to the trigger()
331          * function will also cause the browser to submit the form. This
332          * default action can be prevented by returning false from one of
333          * the functions bound to the event.
334          *
335          * You can also trigger custom events registered with bind.
336          *
337          * @example $("p").trigger("click")
338          * @before <p click="alert('hello')">Hello</p>
339          * @result alert('hello')
340          *
341          * @example $("p").click(function(event, a, b) {
342          *   // when a normal click fires, a and b are undefined
343          *   // for a trigger like below a refers too "foo" and b refers to "bar"
344          * }).trigger("click", ["foo", "bar"]);
345          * @desc Example of how to pass arbitrary data to an event
346          * 
347          * @example $("p").bind("myEvent",function(event,message1,message2) {
348          *      alert(message1 + ' ' + message2);
349          * });
350          * $("p").trigger("myEvent",["Hello","World"]);
351          * @result alert('Hello World') // One for each paragraph
352          *
353          * @name trigger
354          * @type jQuery
355          * @param String type An event type to trigger.
356          * @param Array data (optional) Additional data to pass as arguments (after the event object) to the event handler
357          * @cat Events
358          */
359         trigger: function( type, data ) {
360                 return this.each(function(){
361                         jQuery.event.trigger( type, data, this );
362                 });
363         },
364
365         /**
366          * Toggle between two function calls every other click.
367          * Whenever a matched element is clicked, the first specified function 
368          * is fired, when clicked again, the second is fired. All subsequent 
369          * clicks continue to rotate through the two functions.
370          *
371          * Use unbind("click") to remove.
372          *
373          * @example $("p").toggle(function(){
374          *   $(this).addClass("selected");
375          * },function(){
376          *   $(this).removeClass("selected");
377          * });
378          * 
379          * @name toggle
380          * @type jQuery
381          * @param Function even The function to execute on every even click.
382          * @param Function odd The function to execute on every odd click.
383          * @cat Events
384          */
385         toggle: function() {
386                 // Save reference to arguments for access in closure
387                 var a = arguments;
388
389                 return this.click(function(e) {
390                         // Figure out which function to execute
391                         this.lastToggle = this.lastToggle == 0 ? 1 : 0;
392                         
393                         // Make sure that clicks stop
394                         e.preventDefault();
395                         
396                         // and execute the function
397                         return a[this.lastToggle].apply( this, [e] ) || false;
398                 });
399         },
400         
401         /**
402          * A method for simulating hovering (moving the mouse on, and off,
403          * an object). This is a custom method which provides an 'in' to a 
404          * frequent task.
405          *
406          * Whenever the mouse cursor is moved over a matched 
407          * element, the first specified function is fired. Whenever the mouse 
408          * moves off of the element, the second specified function fires. 
409          * Additionally, checks are in place to see if the mouse is still within 
410          * the specified element itself (for example, an image inside of a div), 
411          * and if it is, it will continue to 'hover', and not move out 
412          * (a common error in using a mouseout event handler).
413          *
414          * @example $("p").hover(function(){
415          *   $(this).addClass("hover");
416          * },function(){
417          *   $(this).removeClass("hover");
418          * });
419          *
420          * @name hover
421          * @type jQuery
422          * @param Function over The function to fire whenever the mouse is moved over a matched element.
423          * @param Function out The function to fire whenever the mouse is moved off of a matched element.
424          * @cat Events
425          */
426         hover: function(f,g) {
427                 
428                 // A private function for handling mouse 'hovering'
429                 function handleHover(e) {
430                         // Check if mouse(over|out) are still within the same parent element
431                         var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
432         
433                         // Traverse up the tree
434                         while ( p && p != this ) try { p = p.parentNode } catch(e) { p = this; };
435                         
436                         // If we actually just moused on to a sub-element, ignore it
437                         if ( p == this ) return false;
438                         
439                         // Execute the right function
440                         return (e.type == "mouseover" ? f : g).apply(this, [e]);
441                 }
442                 
443                 // Bind the function to the two event listeners
444                 return this.mouseover(handleHover).mouseout(handleHover);
445         },
446         
447         /**
448          * Bind a function to be executed whenever the DOM is ready to be
449          * traversed and manipulated. This is probably the most important 
450          * function included in the event module, as it can greatly improve
451          * the response times of your web applications.
452          *
453          * In a nutshell, this is a solid replacement for using window.onload, 
454          * and attaching a function to that. By using this method, your bound function 
455          * will be called the instant the DOM is ready to be read and manipulated, 
456          * which is when what 99.99% of all JavaScript code needs to run.
457          *
458          * There is one argument passed to the ready event handler: A reference to
459          * the jQuery function. You can name that argument whatever you like, and
460          * can therefore stick with the $ alias without risk of naming collisions.
461          * 
462          * Please ensure you have no code in your &lt;body&gt; onload event handler, 
463          * otherwise $(document).ready() may not fire.
464          *
465          * You can have as many $(document).ready events on your page as you like.
466          * The functions are then executed in the order they were added.
467          *
468          * @example $(document).ready(function(){ Your code here... });
469          *
470          * @example jQuery(function($) {
471          *   // Your code using failsafe $ alias here...
472          * });
473          * @desc Uses both the [[Core#.24.28_fn_.29|shortcut]] for $(document).ready() and the argument
474          * to write failsafe jQuery code using the $ alias, without relying on the
475          * global alias.
476          *
477          * @name ready
478          * @type jQuery
479          * @param Function fn The function to be executed when the DOM is ready.
480          * @cat Events
481          * @see $.noConflict()
482          * @see $(Function)
483          */
484         ready: function(f) {
485                 // If the DOM is already ready
486                 if ( jQuery.isReady )
487                         // Execute the function immediately
488                         f.apply( document, [jQuery] );
489                         
490                 // Otherwise, remember the function for later
491                 else {
492                         // Add the function to the wait list
493                         jQuery.readyList.push( function() { return f.apply(this, [jQuery]) } );
494                 }
495         
496                 return this;
497         }
498 });
499
500 jQuery.extend({
501         /*
502          * All the code that makes DOM Ready work nicely.
503          */
504         isReady: false,
505         readyList: [],
506         
507         // Handle when the DOM is ready
508         ready: function() {
509                 // Make sure that the DOM is not already loaded
510                 if ( !jQuery.isReady ) {
511                         // Remember that the DOM is ready
512                         jQuery.isReady = true;
513                         
514                         // If there are functions bound, to execute
515                         if ( jQuery.readyList ) {
516                                 // Execute all of them
517                                 jQuery.each( jQuery.readyList, function(){
518                                         this.apply( document );
519                                 });
520                                 
521                                 // Reset the list of functions
522                                 jQuery.readyList = null;
523                         }
524                         // Remove event lisenter to avoid memory leak
525                         if ( jQuery.browser.mozilla || jQuery.browser.opera )
526                                 document.removeEventListener( "DOMContentLoaded", jQuery.ready, false );
527                 }
528         }
529 });
530
531 new function(){
532
533         /**
534          * Bind a function to the scroll event of each matched element.
535          *
536          * @example $("p").scroll( function() { alert("Hello"); } );
537          * @before <p>Hello</p>
538          * @result <p onscroll="alert('Hello');">Hello</p>
539          *
540          * @name scroll
541          * @type jQuery
542          * @param Function fn A function to bind to the scroll event on each of the matched elements.
543          * @cat Events
544          */
545
546         /**
547          * Bind a function to the submit event of each matched element.
548          *
549          * @example $("#myform").submit( function() {
550          *   return $("input", this).val().length > 0;
551          * } );
552          * @before <form id="myform"><input /></form>
553          * @desc Prevents the form submission when the input has no value entered.
554          *
555          * @name submit
556          * @type jQuery
557          * @param Function fn A function to bind to the submit event on each of the matched elements.
558          * @cat Events
559          */
560
561         /**
562          * Trigger the submit event of each matched element. This causes all of the functions
563          * that have been bound to that submit event to be executed, and calls the browser's
564          * default submit action on the matching element(s). This default action can be prevented
565          * by returning false from one of the functions bound to the submit event.
566          *
567          * Note: This does not execute the submit method of the form element! If you need to
568          * submit the form via code, you have to use the DOM method, eg. $("form")[0].submit();
569          *
570          * @example $("form").submit();
571          * @desc Triggers all submit events registered to the matched form(s), and submits them.
572          *
573          * @name submit
574          * @type jQuery
575          * @cat Events
576          */
577
578         /**
579          * Bind a function to the focus event of each matched element.
580          *
581          * @example $("p").focus( function() { alert("Hello"); } );
582          * @before <p>Hello</p>
583          * @result <p onfocus="alert('Hello');">Hello</p>
584          *
585          * @name focus
586          * @type jQuery
587          * @param Function fn A function to bind to the focus event on each of the matched elements.
588          * @cat Events
589          */
590
591         /**
592          * Trigger the focus event of each matched element. This causes all of the functions
593          * that have been bound to thet focus event to be executed.
594          *
595          * Note: This does not execute the focus method of the underlying elements! If you need to
596          * focus an element via code, you have to use the DOM method, eg. $("#myinput")[0].focus();
597          *
598          * @example $("p").focus();
599          * @before <p onfocus="alert('Hello');">Hello</p>
600          * @result alert('Hello');
601          *
602          * @name focus
603          * @type jQuery
604          * @cat Events
605          */
606
607         /**
608          * Bind a function to the keydown event of each matched element.
609          *
610          * @example $("p").keydown( function() { alert("Hello"); } );
611          * @before <p>Hello</p>
612          * @result <p onkeydown="alert('Hello');">Hello</p>
613          *
614          * @name keydown
615          * @type jQuery
616          * @param Function fn A function to bind to the keydown event on each of the matched elements.
617          * @cat Events
618          */
619
620         /**
621          * Bind a function to the dblclick event of each matched element.
622          *
623          * @example $("p").dblclick( function() { alert("Hello"); } );
624          * @before <p>Hello</p>
625          * @result <p ondblclick="alert('Hello');">Hello</p>
626          *
627          * @name dblclick
628          * @type jQuery
629          * @param Function fn A function to bind to the dblclick event on each of the matched elements.
630          * @cat Events
631          */
632
633         /**
634          * Bind a function to the keypress event of each matched element.
635          *
636          * @example $("p").keypress( function() { alert("Hello"); } );
637          * @before <p>Hello</p>
638          * @result <p onkeypress="alert('Hello');">Hello</p>
639          *
640          * @name keypress
641          * @type jQuery
642          * @param Function fn A function to bind to the keypress event on each of the matched elements.
643          * @cat Events
644          */
645
646         /**
647          * Bind a function to the error event of each matched element.
648          *
649          * @example $("p").error( function() { alert("Hello"); } );
650          * @before <p>Hello</p>
651          * @result <p onerror="alert('Hello');">Hello</p>
652          *
653          * @name error
654          * @type jQuery
655          * @param Function fn A function to bind to the error event on each of the matched elements.
656          * @cat Events
657          */
658
659         /**
660          * Bind a function to the blur event of each matched element.
661          *
662          * @example $("p").blur( function() { alert("Hello"); } );
663          * @before <p>Hello</p>
664          * @result <p onblur="alert('Hello');">Hello</p>
665          *
666          * @name blur
667          * @type jQuery
668          * @param Function fn A function to bind to the blur event on each of the matched elements.
669          * @cat Events
670          */
671
672         /**
673          * Trigger the blur event of each matched element. This causes all of the functions
674          * that have been bound to that blur event to be executed, and calls the browser's
675          * default blur action on the matching element(s). This default action can be prevented
676          * by returning false from one of the functions bound to the blur event.
677          *
678          * Note: This does not execute the blur method of the underlying elements! If you need to
679          * blur an element via code, you have to use the DOM method, eg. $("#myinput")[0].blur();
680          *
681          * @example $("p").blur();
682          * @before <p onblur="alert('Hello');">Hello</p>
683          * @result alert('Hello');
684          *
685          * @name blur
686          * @type jQuery
687          * @cat Events
688          */
689
690         /**
691          * Bind a function to the load event of each matched element.
692          *
693          * @example $("p").load( function() { alert("Hello"); } );
694          * @before <p>Hello</p>
695          * @result <p onload="alert('Hello');">Hello</p>
696          *
697          * @name load
698          * @type jQuery
699          * @param Function fn A function to bind to the load event on each of the matched elements.
700          * @cat Events
701          */
702
703         /**
704          * Bind a function to the select event of each matched element.
705          *
706          * @example $("p").select( function() { alert("Hello"); } );
707          * @before <p>Hello</p>
708          * @result <p onselect="alert('Hello');">Hello</p>
709          *
710          * @name select
711          * @type jQuery
712          * @param Function fn A function to bind to the select event on each of the matched elements.
713          * @cat Events
714          */
715
716         /**
717          * Trigger the select event of each matched element. This causes all of the functions
718          * that have been bound to that select event to be executed, and calls the browser's
719          * default select action on the matching element(s). This default action can be prevented
720          * by returning false from one of the functions bound to the select event.
721          *
722          * @example $("p").select();
723          * @before <p onselect="alert('Hello');">Hello</p>
724          * @result alert('Hello');
725          *
726          * @name select
727          * @type jQuery
728          * @cat Events
729          */
730
731         /**
732          * Bind a function to the mouseup event of each matched element.
733          *
734          * @example $("p").mouseup( function() { alert("Hello"); } );
735          * @before <p>Hello</p>
736          * @result <p onmouseup="alert('Hello');">Hello</p>
737          *
738          * @name mouseup
739          * @type jQuery
740          * @param Function fn A function to bind to the mouseup event on each of the matched elements.
741          * @cat Events
742          */
743
744         /**
745          * Bind a function to the unload event of each matched element.
746          *
747          * @example $("p").unload( function() { alert("Hello"); } );
748          * @before <p>Hello</p>
749          * @result <p onunload="alert('Hello');">Hello</p>
750          *
751          * @name unload
752          * @type jQuery
753          * @param Function fn A function to bind to the unload event on each of the matched elements.
754          * @cat Events
755          */
756
757         /**
758          * Bind a function to the change event of each matched element.
759          *
760          * @example $("p").change( function() { alert("Hello"); } );
761          * @before <p>Hello</p>
762          * @result <p onchange="alert('Hello');">Hello</p>
763          *
764          * @name change
765          * @type jQuery
766          * @param Function fn A function to bind to the change event on each of the matched elements.
767          * @cat Events
768          */
769
770         /**
771          * Bind a function to the mouseout event of each matched element.
772          *
773          * @example $("p").mouseout( function() { alert("Hello"); } );
774          * @before <p>Hello</p>
775          * @result <p onmouseout="alert('Hello');">Hello</p>
776          *
777          * @name mouseout
778          * @type jQuery
779          * @param Function fn A function to bind to the mouseout event on each of the matched elements.
780          * @cat Events
781          */
782
783         /**
784          * Bind a function to the keyup event of each matched element.
785          *
786          * @example $("p").keyup( function() { alert("Hello"); } );
787          * @before <p>Hello</p>
788          * @result <p onkeyup="alert('Hello');">Hello</p>
789          *
790          * @name keyup
791          * @type jQuery
792          * @param Function fn A function to bind to the keyup event on each of the matched elements.
793          * @cat Events
794          */
795
796         /**
797          * Bind a function to the click event of each matched element.
798          *
799          * @example $("p").click( function() { alert("Hello"); } );
800          * @before <p>Hello</p>
801          * @result <p onclick="alert('Hello');">Hello</p>
802          *
803          * @name click
804          * @type jQuery
805          * @param Function fn A function to bind to the click event on each of the matched elements.
806          * @cat Events
807          */
808
809         /**
810          * Trigger the click event of each matched element. This causes all of the functions
811          * that have been bound to thet click event to be executed.
812          *
813          * @example $("p").click();
814          * @before <p onclick="alert('Hello');">Hello</p>
815          * @result alert('Hello');
816          *
817          * @name click
818          * @type jQuery
819          * @cat Events
820          */
821
822         /**
823          * Bind a function to the resize event of each matched element.
824          *
825          * @example $("p").resize( function() { alert("Hello"); } );
826          * @before <p>Hello</p>
827          * @result <p onresize="alert('Hello');">Hello</p>
828          *
829          * @name resize
830          * @type jQuery
831          * @param Function fn A function to bind to the resize event on each of the matched elements.
832          * @cat Events
833          */
834
835         /**
836          * Bind a function to the mousemove event of each matched element.
837          *
838          * @example $("p").mousemove( function() { alert("Hello"); } );
839          * @before <p>Hello</p>
840          * @result <p onmousemove="alert('Hello');">Hello</p>
841          *
842          * @name mousemove
843          * @type jQuery
844          * @param Function fn A function to bind to the mousemove event on each of the matched elements.
845          * @cat Events
846          */
847
848         /**
849          * Bind a function to the mousedown event of each matched element.
850          *
851          * @example $("p").mousedown( function() { alert("Hello"); } );
852          * @before <p>Hello</p>
853          * @result <p onmousedown="alert('Hello');">Hello</p>
854          *
855          * @name mousedown
856          * @type jQuery
857          * @param Function fn A function to bind to the mousedown event on each of the matched elements.
858          * @cat Events
859          */
860          
861         /**
862          * Bind a function to the mouseover event of each matched element.
863          *
864          * @example $("p").mouseover( function() { alert("Hello"); } );
865          * @before <p>Hello</p>
866          * @result <p onmouseover="alert('Hello');">Hello</p>
867          *
868          * @name mouseover
869          * @type jQuery
870          * @param Function fn A function to bind to the mousedown event on each of the matched elements.
871          * @cat Events
872          */
873         jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
874                 "mousedown,mouseup,mousemove,mouseover,mouseout,change,select," + 
875                 "submit,keydown,keypress,keyup,error").split(","), function(i,o){
876                 
877                 // Handle event binding
878                 jQuery.fn[o] = function(f){
879                         return f ? this.bind(o, f) : this.trigger(o);
880                 };
881                         
882         });
883         
884         // If Mozilla is used
885         if ( jQuery.browser.mozilla || jQuery.browser.opera )
886                 // Use the handy event callback
887                 document.addEventListener( "DOMContentLoaded", jQuery.ready, false );
888         
889         // If IE is used, use the excellent hack by Matthias Miller
890         // http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
891         else if ( jQuery.browser.msie ) {
892         
893                 // Only works if you document.write() it
894                 document.write("<scr" + "ipt id=__ie_init defer=true " + 
895                         "src=//:><\/script>");
896         
897                 // Use the defer script hack
898                 var script = document.getElementById("__ie_init");
899                 
900                 // script does not exist if jQuery is loaded dynamically
901                 if ( script ) 
902                         script.onreadystatechange = function() {
903                                 if ( this.readyState != "complete" ) return;
904                                 this.parentNode.removeChild( this );
905                                 jQuery.ready();
906                         };
907         
908                 // Clear from memory
909                 script = null;
910         
911         // If Safari  is used
912         } else if ( jQuery.browser.safari )
913                 // Continually check to see if the document.readyState is valid
914                 jQuery.safariTimer = setInterval(function(){
915                         // loaded and complete are both valid states
916                         if ( document.readyState == "loaded" || 
917                                 document.readyState == "complete" ) {
918         
919                                 // If either one are found, remove the timer
920                                 clearInterval( jQuery.safariTimer );
921                                 jQuery.safariTimer = null;
922         
923                                 // and execute any waiting functions
924                                 jQuery.ready();
925                         }
926                 }, 10); 
927
928         // A fallback to window.onload, that will always work
929         jQuery.event.add( window, "load", jQuery.ready );
930         
931 };
932
933 // Clean up after IE to avoid memory leaks
934 if (jQuery.browser.msie)
935         jQuery(window).one("unload", function() {
936                 var global = jQuery.event.global;
937                 for ( var type in global ) {
938                         var els = global[type], i = els.length;
939                         if ( i && type != 'unload' )
940                                 do
941                                         jQuery.event.remove(els[i-1], type);
942                                 while (--i);
943                 }
944         });