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