3 // We're overriding the old toggle function, so
4 // remember it for later
5 _toggle: jQuery.fn.toggle,
8 * Toggle between two function calls every other click.
9 * Whenever a matched element is clicked, the first specified function
10 * is fired, when clicked again, the second is fired. All subsequent
11 * clicks continue to rotate through the two functions.
13 * Use unbind("click") to remove.
15 * @example $("p").toggle(function(){
16 * $(this).addClass("selected");
18 * $(this).removeClass("selected");
23 * @param Function even The function to execute on every even click.
24 * @param Function odd The function to execute on every odd click.
28 // save reference to arguments for access in closure
30 return typeof a[0] == "function" && typeof a[1] == "function" ? this.click(function(e) {
31 // Figure out which function to execute
32 this.lastToggle = this.lastToggle == 0 ? 1 : 0;
34 // Make sure that clicks stop
37 // and execute the function
38 return a[this.lastToggle].apply( this, [e] ) || false;
41 // Otherwise, execute the old toggle function
42 this._toggle.apply( this, arguments );
46 * A method for simulating hovering (moving the mouse on, and off,
47 * an object). This is a custom method which provides an 'in' to a
50 * Whenever the mouse cursor is moved over a matched
51 * element, the first specified function is fired. Whenever the mouse
52 * moves off of the element, the second specified function fires.
53 * Additionally, checks are in place to see if the mouse is still within
54 * the specified element itself (for example, an image inside of a div),
55 * and if it is, it will continue to 'hover', and not move out
56 * (a common error in using a mouseout event handler).
58 * @example $("p").hover(function(){
59 * $(this).addClass("over");
61 * $(this).addClass("out");
66 * @param Function over The function to fire whenever the mouse is moved over a matched element.
67 * @param Function out The function to fire whenever the mouse is moved off of a matched element.
70 hover: function(f,g) {
72 // A private function for haandling mouse 'hovering'
73 function handleHover(e) {
74 // Check if mouse(over|out) are still within the same parent element
75 var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
77 // Traverse up the tree
78 while ( p && p != this ) try { p = p.parentNode } catch(e) { p = this; };
80 // If we actually just moused on to a sub-element, ignore it
81 if ( p == this ) return false;
83 // Execute the right function
84 return (e.type == "mouseover" ? f : g).apply(this, [e]);
87 // Bind the function to the two event listeners
88 return this.mouseover(handleHover).mouseout(handleHover);
92 * Bind a function to be executed whenever the DOM is ready to be
93 * traversed and manipulated. This is probably the most important
94 * function included in the event module, as it can greatly improve
95 * the response times of your web applications.
97 * In a nutshell, this is a solid replacement for using window.onload,
98 * and attaching a function to that. By using this method, your bound Function
99 * will be called the instant the DOM is ready to be read and manipulated,
100 * which is exactly what 99.99% of all Javascript code needs to run.
102 * Please ensure you have no code in your <body> onload event handler,
103 * otherwise $(document).ready() may not fire.
105 * You can have as many $(document).ready events on your page as you like.
106 * The functions are then executed in the order they were added.
108 * @example $(document).ready(function(){ Your code here... });
112 * @param Function fn The function to be executed when the DOM is ready.
116 // If the DOM is already ready
117 if ( jQuery.isReady )
118 // Execute the function immediately
121 // Otherwise, remember the function for later
123 // Add the function to the wait list
124 jQuery.readyList.push( f );
133 * All the code that makes DOM Ready work nicely.
138 // Handle when the DOM is ready
140 // Make sure that the DOM is not already loaded
141 if ( !jQuery.isReady ) {
142 // Remember that the DOM is ready
143 jQuery.isReady = true;
145 // If there are functions bound, to execute
146 if ( jQuery.readyList ) {
147 // Execute all of them
148 for ( var i = 0; i < jQuery.readyList.length; i++ )
149 jQuery.readyList[i].apply( document );
151 // Reset the list of functions
152 jQuery.readyList = null;
154 // Remove event lisenter to avoid memory leak
155 if ( jQuery.browser.mozilla || jQuery.browser.opera )
156 document.removeEventListener( "DOMContentLoaded", jQuery.ready, false );
164 * Bind a function to the scroll event of each matched element.
166 * @example $("p").scroll( function() { alert("Hello"); } );
167 * @before <p>Hello</p>
168 * @result <p onscroll="alert('Hello');">Hello</p>
172 * @param Function fn A function to bind to the scroll event on each of the matched elements.
173 * @cat Events/Browser
177 * Trigger the scroll event of each matched element. This causes all of the functions
178 * that have been bound to thet scroll event to be executed.
180 * @example $("p").scroll();
181 * @before <p onscroll="alert('Hello');">Hello</p>
182 * @result alert('Hello');
186 * @cat Events/Browser
190 * Bind a function to the submit event of each matched element.
192 * @example $("#myform").submit( function() {
193 * return $("input", this).val().length > 0;
195 * @before <form id="myform"><input /></form>
196 * @desc Prevents the form submission when the input has no value entered.
200 * @param Function fn A function to bind to the submit event on each of the matched elements.
205 * Trigger the submit event of each matched element. This causes all of the functions
206 * that have been bound to thet submit event to be executed.
208 * Note: This does not execute the submit method of the form element! If you need to
209 * submit the form via code, you have to use the DOM method, eg. $("form")[0].submit();
211 * @example $("form").submit();
212 * @desc Triggers all submit events registered for forms, but does not submit the form
220 * Bind a function to the focus event of each matched element.
222 * @example $("p").focus( function() { alert("Hello"); } );
223 * @before <p>Hello</p>
224 * @result <p onfocus="alert('Hello');">Hello</p>
228 * @param Function fn A function to bind to the focus event on each of the matched elements.
233 * Trigger the focus event of each matched element. This causes all of the functions
234 * that have been bound to thet focus event to be executed.
236 * Note: This does not execute the focus method of the underlying elements! If you need to
237 * focus an element via code, you have to use the DOM method, eg. $("#myinput")[0].focus();
239 * @example $("p").focus();
240 * @before <p onfocus="alert('Hello');">Hello</p>
241 * @result alert('Hello');
249 * Bind a function to the keydown event of each matched element.
251 * @example $("p").keydown( function() { alert("Hello"); } );
252 * @before <p>Hello</p>
253 * @result <p onkeydown="alert('Hello');">Hello</p>
257 * @param Function fn A function to bind to the keydown event on each of the matched elements.
258 * @cat Events/Keyboard
262 * Trigger the keydown event of each matched element. This causes all of the functions
263 * that have been bound to thet keydown event to be executed.
265 * @example $("p").keydown();
266 * @before <p onkeydown="alert('Hello');">Hello</p>
267 * @result alert('Hello');
271 * @cat Events/Keyboard
275 * Bind a function to the dblclick event of each matched element.
277 * @example $("p").dblclick( function() { alert("Hello"); } );
278 * @before <p>Hello</p>
279 * @result <p ondblclick="alert('Hello');">Hello</p>
283 * @param Function fn A function to bind to the dblclick event on each of the matched elements.
288 * Trigger the dblclick event of each matched element. This causes all of the functions
289 * that have been bound to thet dblclick event to be executed.
291 * @example $("p").dblclick();
292 * @before <p ondblclick="alert('Hello');">Hello</p>
293 * @result alert('Hello');
301 * Bind a function to the keypress event of each matched element.
303 * @example $("p").keypress( function() { alert("Hello"); } );
304 * @before <p>Hello</p>
305 * @result <p onkeypress="alert('Hello');">Hello</p>
309 * @param Function fn A function to bind to the keypress event on each of the matched elements.
310 * @cat Events/Keyboard
314 * Trigger the keypress event of each matched element. This causes all of the functions
315 * that have been bound to thet keypress event to be executed.
317 * @example $("p").keypress();
318 * @before <p onkeypress="alert('Hello');">Hello</p>
319 * @result alert('Hello');
323 * @cat Events/Keyboard
327 * Bind a function to the error event of each matched element.
329 * @example $("p").error( function() { alert("Hello"); } );
330 * @before <p>Hello</p>
331 * @result <p onerror="alert('Hello');">Hello</p>
335 * @param Function fn A function to bind to the error event on each of the matched elements.
336 * @cat Events/Browser
340 * Trigger the error event of each matched element. This causes all of the functions
341 * that have been bound to thet error event to be executed.
343 * @example $("p").error();
344 * @before <p onerror="alert('Hello');">Hello</p>
345 * @result alert('Hello');
349 * @cat Events/Browser
353 * Bind a function to the blur event of each matched element.
355 * @example $("p").blur( function() { alert("Hello"); } );
356 * @before <p>Hello</p>
357 * @result <p onblur="alert('Hello');">Hello</p>
361 * @param Function fn A function to bind to the blur event on each of the matched elements.
366 * Trigger the blur event of each matched element. This causes all of the functions
367 * that have been bound to thet blur event to be executed.
369 * Note: This does not execute the blur method of the underlying elements! If you need to
370 * blur an element via code, you have to use the DOM method, eg. $("#myinput")[0].blur();
372 * @example $("p").blur();
373 * @before <p onblur="alert('Hello');">Hello</p>
374 * @result alert('Hello');
382 * Bind a function to the load event of each matched element.
384 * @example $("p").load( function() { alert("Hello"); } );
385 * @before <p>Hello</p>
386 * @result <p onload="alert('Hello');">Hello</p>
390 * @param Function fn A function to bind to the load event on each of the matched elements.
391 * @cat Events/Browser
395 * Trigger the load event of each matched element. This causes all of the functions
396 * that have been bound to thet load event to be executed.
398 * Marked as private: Calling load() without arguments throws exception because the ajax load
399 * does not handle it.
401 * @example $("p").load();
402 * @before <p onload="alert('Hello');">Hello</p>
403 * @result alert('Hello');
408 * @cat Events/Browser
412 * Bind a function to the select event of each matched element.
414 * @example $("p").select( function() { alert("Hello"); } );
415 * @before <p>Hello</p>
416 * @result <p onselect="alert('Hello');">Hello</p>
420 * @param Function fn A function to bind to the select event on each of the matched elements.
425 * Trigger the select event of each matched element. This causes all of the functions
426 * that have been bound to thet select event to be executed.
428 * @example $("p").select();
429 * @before <p onselect="alert('Hello');">Hello</p>
430 * @result alert('Hello');
438 * Bind a function to the mouseup event of each matched element.
440 * @example $("p").mouseup( function() { alert("Hello"); } );
441 * @before <p>Hello</p>
442 * @result <p onmouseup="alert('Hello');">Hello</p>
446 * @param Function fn A function to bind to the mouseup event on each of the matched elements.
451 * Trigger the mouseup event of each matched element. This causes all of the functions
452 * that have been bound to thet mouseup event to be executed.
454 * @example $("p").mouseup();
455 * @before <p onmouseup="alert('Hello');">Hello</p>
456 * @result alert('Hello');
464 * Bind a function to the unload event of each matched element.
466 * @example $("p").unload( function() { alert("Hello"); } );
467 * @before <p>Hello</p>
468 * @result <p onunload="alert('Hello');">Hello</p>
472 * @param Function fn A function to bind to the unload event on each of the matched elements.
473 * @cat Events/Browser
477 * Trigger the unload event of each matched element. This causes all of the functions
478 * that have been bound to thet unload event to be executed.
480 * @example $("p").unload();
481 * @before <p onunload="alert('Hello');">Hello</p>
482 * @result alert('Hello');
486 * @cat Events/Browser
490 * Bind a function to the change event of each matched element.
492 * @example $("p").change( function() { alert("Hello"); } );
493 * @before <p>Hello</p>
494 * @result <p onchange="alert('Hello');">Hello</p>
498 * @param Function fn A function to bind to the change event on each of the matched elements.
503 * Trigger the change event of each matched element. This causes all of the functions
504 * that have been bound to thet change event to be executed.
506 * @example $("p").change();
507 * @before <p onchange="alert('Hello');">Hello</p>
508 * @result alert('Hello');
516 * Bind a function to the mouseout event of each matched element.
518 * @example $("p").mouseout( function() { alert("Hello"); } );
519 * @before <p>Hello</p>
520 * @result <p onmouseout="alert('Hello');">Hello</p>
524 * @param Function fn A function to bind to the mouseout event on each of the matched elements.
529 * Trigger the mouseout event of each matched element. This causes all of the functions
530 * that have been bound to thet mouseout event to be executed.
532 * @example $("p").mouseout();
533 * @before <p onmouseout="alert('Hello');">Hello</p>
534 * @result alert('Hello');
542 * Bind a function to the keyup event of each matched element.
544 * @example $("p").keyup( function() { alert("Hello"); } );
545 * @before <p>Hello</p>
546 * @result <p onkeyup="alert('Hello');">Hello</p>
550 * @param Function fn A function to bind to the keyup event on each of the matched elements.
551 * @cat Events/Keyboard
555 * Trigger the keyup event of each matched element. This causes all of the functions
556 * that have been bound to thet keyup event to be executed.
558 * @example $("p").keyup();
559 * @before <p onkeyup="alert('Hello');">Hello</p>
560 * @result alert('Hello');
564 * @cat Events/Keyboard
568 * Bind a function to the click event of each matched element.
570 * @example $("p").click( function() { alert("Hello"); } );
571 * @before <p>Hello</p>
572 * @result <p onclick="alert('Hello');">Hello</p>
576 * @param Function fn A function to bind to the click event on each of the matched elements.
581 * Trigger the click event of each matched element. This causes all of the functions
582 * that have been bound to thet click event to be executed.
584 * @example $("p").click();
585 * @before <p onclick="alert('Hello');">Hello</p>
586 * @result alert('Hello');
594 * Bind a function to the resize event of each matched element.
596 * @example $("p").resize( function() { alert("Hello"); } );
597 * @before <p>Hello</p>
598 * @result <p onresize="alert('Hello');">Hello</p>
602 * @param Function fn A function to bind to the resize event on each of the matched elements.
603 * @cat Events/Browser
607 * Trigger the resize event of each matched element. This causes all of the functions
608 * that have been bound to thet resize event to be executed.
610 * @example $("p").resize();
611 * @before <p onresize="alert('Hello');">Hello</p>
612 * @result alert('Hello');
616 * @cat Events/Browser
620 * Bind a function to the mousemove event of each matched element.
622 * @example $("p").mousemove( function() { alert("Hello"); } );
623 * @before <p>Hello</p>
624 * @result <p onmousemove="alert('Hello');">Hello</p>
628 * @param Function fn A function to bind to the mousemove event on each of the matched elements.
633 * Trigger the mousemove event of each matched element. This causes all of the functions
634 * that have been bound to thet mousemove event to be executed.
636 * @example $("p").mousemove();
637 * @before <p onmousemove="alert('Hello');">Hello</p>
638 * @result alert('Hello');
646 * Bind a function to the mousedown event of each matched element.
648 * @example $("p").mousedown( function() { alert("Hello"); } );
649 * @before <p>Hello</p>
650 * @result <p onmousedown="alert('Hello');">Hello</p>
654 * @param Function fn A function to bind to the mousedown event on each of the matched elements.
659 * Trigger the mousedown event of each matched element. This causes all of the functions
660 * that have been bound to thet mousedown event to be executed.
662 * @example $("p").mousedown();
663 * @before <p onmousedown="alert('Hello');">Hello</p>
664 * @result alert('Hello');
672 * Bind a function to the mouseover event of each matched element.
674 * @example $("p").mouseover( function() { alert("Hello"); } );
675 * @before <p>Hello</p>
676 * @result <p onmouseover="alert('Hello');">Hello</p>
680 * @param Function fn A function to bind to the mousedown event on each of the matched elements.
685 * Trigger the mouseover event of each matched element. This causes all of the functions
686 * that have been bound to thet mousedown event to be executed.
688 * @example $("p").mouseover();
689 * @before <p onmouseover="alert('Hello');">Hello</p>
690 * @result alert('Hello');
697 var e = ("blur,focus,load,resize,scroll,unload,click,dblclick," +
698 "mousedown,mouseup,mousemove,mouseover,mouseout,change,select," +
699 "submit,keydown,keypress,keyup,error").split(",");
701 // Go through all the event names, but make sure that
702 // it is enclosed properly
703 for ( var i = 0; i < e.length; i++ ) new function(){
707 // Handle event binding
708 jQuery.fn[o] = function(f){
709 return f ? this.bind(o, f) : this.trigger(o);
712 // Handle event unbinding
714 jQuery.fn["un"+o] = function(f){ return this.unbind(o, f); };
716 // Finally, handle events that only fire once
718 jQuery.fn["one"+o] = function(f){
719 // save cloned reference to this
720 var element = jQuery(this);
721 var handler = function() {
722 // unbind itself when executed
723 element.unbind(o, handler);
725 // apply original handler with the same arguments
726 return f.apply(this, arguments);
728 return this.bind(o, handler);
733 // If Mozilla is used
734 if ( jQuery.browser.mozilla || jQuery.browser.opera ) {
735 // Use the handy event callback
736 document.addEventListener( "DOMContentLoaded", jQuery.ready, false );
738 // If IE is used, use the excellent hack by Matthias Miller
739 // http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
740 } else if ( jQuery.browser.msie ) {
742 // Only works if you document.write() it
743 document.write("<scr" + "ipt id=__ie_init defer=true " +
744 "src=//:><\/script>");
746 // Use the defer script hack
747 var script = document.getElementById("__ie_init");
748 if (script) // script does not exist if jQuery is loaded dynamically
749 script.onreadystatechange = function() {
750 if ( this.readyState != "complete" ) return;
751 this.parentNode.removeChild( this );
759 } else if ( jQuery.browser.safari ) {
760 // Continually check to see if the document.readyState is valid
761 jQuery.safariTimer = setInterval(function(){
762 // loaded and complete are both valid states
763 if ( document.readyState == "loaded" ||
764 document.readyState == "complete" ) {
766 // If either one are found, remove the timer
767 clearInterval( jQuery.safariTimer );
768 jQuery.safariTimer = null;
770 // and execute any waiting functions
776 // A fallback to window.onload, that will always work
777 jQuery.event.add( window, "load", jQuery.ready );
781 // Clean up after IE to avoid memory leaks
782 if (jQuery.browser.msie) jQuery(window).unload(function() {
783 var event = jQuery.event, global = event.global;
784 for (var type in global) {
785 var els = global[type], i = els.length;
786 if (i>0) do if (type != 'unload') event.remove(els[i-1], type); while (--i);