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