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