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