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.
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 )
16 // if data is passed, bind to handler
20 // Make sure that the function being executed has a unique ID
22 handler.guid = this.guid++;
24 // Init the element's event structure
28 // Get the current list of functions bound to this event
29 var handlers = element.events[type];
31 // If it hasn't been initialized yet
33 // Init the event handler queue
34 handlers = element.events[type] = {};
36 // Remember an existing handler, if it's already there
37 if (element["on" + type])
38 handlers[0] = element["on" + type];
41 // Add the function to the element's handler list
42 handlers[handler.guid] = handler;
44 // And bind the global event handler to the element
45 element["on" + type] = this.handle;
47 // Remember the function in a global list (for triggering)
48 if (!this.global[type])
49 this.global[type] = [];
50 this.global[type].push( element );
56 // Detach an event or set of events from an element
57 remove: function(element, type, handler) {
59 if ( type && type.type )
60 delete element.events[ type.type ][ type.handler.guid ];
61 else if (type && element.events[type])
63 delete element.events[type][handler.guid];
65 for ( var i in element.events[type] )
66 delete element.events[type][i];
68 for ( var j in element.events )
69 this.remove( element, j );
72 trigger: function(type, data, element) {
73 // Clone the incoming data, if any
74 data = jQuery.makeArray(data || []);
76 // Handle a global trigger
78 jQuery.each( this.global[type] || [], function(){
79 jQuery.event.trigger( type, data, this );
82 // Handle triggering a single element
84 var handler = element["on" + type ], val,
85 fn = jQuery.isFunction( element[ type ] );
88 // Pass along a fake event
89 data.unshift( this.fix({ type: type, target: element }) );
92 if ( (val = handler.apply( element, data )) !== false )
93 this.triggered = true;
96 if ( fn && val !== false )
99 this.triggered = false;
103 handle: function(event) {
104 // Handle the second event of a trigger and when
105 // an event is called after a page has unloaded
106 if ( typeof jQuery == "undefined" || jQuery.event.triggered ) return;
108 // Empty object is for triggered events with no data
109 event = jQuery.event.fix( event || window.event || {} );
111 // returned undefined or false
114 var c = this.events[event.type];
116 var args = [].slice.call( arguments, 1 );
117 args.unshift( event );
120 // Pass in a reference to the handler function itself
121 // So that we can later remove it
122 args[0].handler = c[j];
123 args[0].data = c[j].data;
125 if ( c[j].apply( this, args ) === false ) {
126 event.preventDefault();
127 event.stopPropagation();
132 // Clean up added properties in IE to prevent memory leak
133 if (jQuery.browser.msie) event.target = event.preventDefault = event.stopPropagation = event.handler = event.data = null;
138 fix: function(event) {
139 // Fix target property, if necessary
140 if ( !event.target && event.srcElement )
141 event.target = event.srcElement;
143 // Calculate pageX/Y if missing and clientX/Y available
144 if ( event.pageX == undefined && event.clientX != undefined ) {
145 var e = document.documentElement, b = document.body;
146 event.pageX = event.clientX + (e.scrollLeft || b.scrollLeft);
147 event.pageY = event.clientY + (e.scrollTop || b.scrollTop);
150 // check if target is a textnode (safari)
151 if (jQuery.browser.safari && event.target.nodeType == 3) {
152 // store a copy of the original event object
153 // and clone because target is read only
154 var originalEvent = event;
155 event = jQuery.extend({}, originalEvent);
157 // get parentnode from textnode
158 event.target = originalEvent.target.parentNode;
160 // add preventDefault and stopPropagation since
161 // they will not work on the clone
162 event.preventDefault = function() {
163 return originalEvent.preventDefault();
165 event.stopPropagation = function() {
166 return originalEvent.stopPropagation();
170 // fix preventDefault and stopPropagation
171 if (!event.preventDefault)
172 event.preventDefault = function() {
173 this.returnValue = false;
176 if (!event.stopPropagation)
177 event.stopPropagation = function() {
178 this.cancelBubble = true;
188 * Binds a handler to a particular event (like click) for each matched element.
189 * The event handler is passed an event object that you can use to prevent
190 * default behaviour. To stop both default action and event bubbling, your handler
191 * has to return false.
193 * In most cases, you can define your event handlers as anonymous functions
194 * (see first example). In cases where that is not possible, you can pass additional
195 * data as the second paramter (and the handler function as the third), see
198 * @example $("p").bind("click", function(){
199 * alert( $(this).text() );
201 * @before <p>Hello</p>
202 * @result alert("Hello")
204 * @example function handler(event) {
205 * alert(event.data.foo);
207 * $("p").bind("click", {foo: "bar"}, handler)
208 * @result alert("bar")
209 * @desc Pass some additional data to the event handler.
211 * @example $("form").bind("submit", function() { return false; })
212 * @desc Cancel a default action and prevent it from bubbling by returning false
213 * from your function.
215 * @example $("form").bind("submit", function(event){
216 * event.preventDefault();
218 * @desc Cancel only the default action by using the preventDefault method.
221 * @example $("form").bind("submit", function(event){
222 * event.stopPropagation();
224 * @desc Stop only an event from bubbling by using the stopPropagation method.
228 * @param String type An event type
229 * @param Object data (optional) Additional data passed to the event handler as event.data
230 * @param Function fn A function to bind to the event on each of the set of matched elements
233 bind: function( type, data, fn ) {
234 return this.each(function(){
235 jQuery.event.add( this, type, fn || data, data );
240 * Binds a handler to a particular event (like click) for each matched element.
241 * The handler is executed only once for each element. Otherwise, the same rules
242 * as described in bind() apply.
243 The event handler is passed an event object that you can use to prevent
244 * default behaviour. To stop both default action and event bubbling, your handler
245 * has to return false.
247 * In most cases, you can define your event handlers as anonymous functions
248 * (see first example). In cases where that is not possible, you can pass additional
249 * data as the second paramter (and the handler function as the third), see
252 * @example $("p").one("click", function(){
253 * alert( $(this).text() );
255 * @before <p>Hello</p>
256 * @result alert("Hello")
260 * @param String type An event type
261 * @param Object data (optional) Additional data passed to the event handler as event.data
262 * @param Function fn A function to bind to the event on each of the set of matched elements
265 one: function( type, data, fn ) {
266 return this.each(function(){
267 jQuery.event.add( this, type, function(event) {
268 jQuery(this).unbind(event);
269 return (fn || data).apply( this, arguments);
275 * The opposite of bind, removes a bound event from each of the matched
278 * Without any arguments, all bound events are removed.
280 * If the type is provided, all bound events of that type are removed.
282 * If the function that was passed to bind is provided as the second argument,
283 * only that specific event handler is removed.
285 * @example $("p").unbind()
286 * @before <p onclick="alert('Hello');">Hello</p>
287 * @result [ <p>Hello</p> ]
289 * @example $("p").unbind( "click" )
290 * @before <p onclick="alert('Hello');">Hello</p>
291 * @result [ <p>Hello</p> ]
293 * @example $("p").unbind( "click", function() { alert("Hello"); } )
294 * @before <p onclick="alert('Hello');">Hello</p>
295 * @result [ <p>Hello</p> ]
299 * @param String type (optional) An event type
300 * @param Function fn (optional) A function to unbind from the event on each of the set of matched elements
303 unbind: function( type, fn ) {
304 return this.each(function(){
305 jQuery.event.remove( this, type, fn );
310 * Trigger a type of event on every matched element.
312 * @example $("p").trigger("click")
313 * @before <p click="alert('hello')">Hello</p>
314 * @result alert('hello')
316 * @example $("p").click(function(event, a, b) {
317 * // when a normal click fires, a and b are undefined
318 * // for a trigger like below a refers too "foo" and b refers to "bar"
319 * }).trigger("click", ["foo", "bar"]);
320 * @desc Example of how to pass arbitrary to an event
322 * @before <p click="alert('hello')">Hello</p>
323 * @result alert('hello')
327 * @param String type An event type to trigger.
328 * @param Array data (optional) Additional data to pass as arguments (after the event object) to the event handler
331 trigger: function( type, data ) {
332 return this.each(function(){
333 jQuery.event.trigger( type, data, this );
338 * Toggle between two function calls every other click.
339 * Whenever a matched element is clicked, the first specified function
340 * is fired, when clicked again, the second is fired. All subsequent
341 * clicks continue to rotate through the two functions.
343 * Use unbind("click") to remove.
345 * @example $("p").toggle(function(){
346 * $(this).addClass("selected");
348 * $(this).removeClass("selected");
353 * @param Function even The function to execute on every even click.
354 * @param Function odd The function to execute on every odd click.
358 // Save reference to arguments for access in closure
361 return this.click(function(e) {
362 // Figure out which function to execute
363 this.lastToggle = this.lastToggle == 0 ? 1 : 0;
365 // Make sure that clicks stop
368 // and execute the function
369 return a[this.lastToggle].apply( this, [e] ) || false;
374 * A method for simulating hovering (moving the mouse on, and off,
375 * an object). This is a custom method which provides an 'in' to a
378 * Whenever the mouse cursor is moved over a matched
379 * element, the first specified function is fired. Whenever the mouse
380 * moves off of the element, the second specified function fires.
381 * Additionally, checks are in place to see if the mouse is still within
382 * the specified element itself (for example, an image inside of a div),
383 * and if it is, it will continue to 'hover', and not move out
384 * (a common error in using a mouseout event handler).
386 * @example $("p").hover(function(){
387 * $(this).addClass("over");
389 * $(this).addClass("out");
394 * @param Function over The function to fire whenever the mouse is moved over a matched element.
395 * @param Function out The function to fire whenever the mouse is moved off of a matched element.
398 hover: function(f,g) {
400 // A private function for handling mouse 'hovering'
401 function handleHover(e) {
402 // Check if mouse(over|out) are still within the same parent element
403 var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
405 // Traverse up the tree
406 while ( p && p != this ) try { p = p.parentNode } catch(e) { p = this; };
408 // If we actually just moused on to a sub-element, ignore it
409 if ( p == this ) return false;
411 // Execute the right function
412 return (e.type == "mouseover" ? f : g).apply(this, [e]);
415 // Bind the function to the two event listeners
416 return this.mouseover(handleHover).mouseout(handleHover);
420 * Bind a function to be executed whenever the DOM is ready to be
421 * traversed and manipulated. This is probably the most important
422 * function included in the event module, as it can greatly improve
423 * the response times of your web applications.
425 * In a nutshell, this is a solid replacement for using window.onload,
426 * and attaching a function to that. By using this method, your bound Function
427 * will be called the instant the DOM is ready to be read and manipulated,
428 * which is exactly what 99.99% of all Javascript code needs to run.
430 * There is one argument passed to the ready event handler: A reference to
431 * the jQuery function. You can name that argument whatever you like, and
432 * can therefore stick with the $ alias without risc of naming collisions.
434 * Please ensure you have no code in your <body> onload event handler,
435 * otherwise $(document).ready() may not fire.
437 * You can have as many $(document).ready events on your page as you like.
438 * The functions are then executed in the order they were added.
440 * @example $(document).ready(function(){ Your code here... });
442 * @example jQuery(function($) {
443 * // Your code using failsafe $ alias here...
445 * @desc Uses both the shortcut for $(document).ready() and the argument
446 * to write failsafe jQuery code using the $ alias, without relying on the
451 * @param Function fn The function to be executed when the DOM is ready.
453 * @see $.noConflict()
457 // If the DOM is already ready
458 if ( jQuery.isReady )
459 // Execute the function immediately
460 f.apply( document, [jQuery] );
462 // Otherwise, remember the function for later
464 // Add the function to the wait list
465 jQuery.readyList.push( function() { return f.apply(this, [jQuery]) } );
474 * All the code that makes DOM Ready work nicely.
479 // Handle when the DOM is ready
481 // Make sure that the DOM is not already loaded
482 if ( !jQuery.isReady ) {
483 // Remember that the DOM is ready
484 jQuery.isReady = true;
486 // If there are functions bound, to execute
487 if ( jQuery.readyList ) {
488 // Execute all of them
489 jQuery.each( jQuery.readyList, function(){
490 this.apply( document );
493 // Reset the list of functions
494 jQuery.readyList = null;
496 // Remove event lisenter to avoid memory leak
497 if ( jQuery.browser.mozilla || jQuery.browser.opera )
498 document.removeEventListener( "DOMContentLoaded", jQuery.ready, false );
506 * Bind a function to the scroll event of each matched element.
508 * @example $("p").scroll( function() { alert("Hello"); } );
509 * @before <p>Hello</p>
510 * @result <p onscroll="alert('Hello');">Hello</p>
514 * @param Function fn A function to bind to the scroll event on each of the matched elements.
519 * Bind a function to the submit event of each matched element.
521 * @example $("#myform").submit( function() {
522 * return $("input", this).val().length > 0;
524 * @before <form id="myform"><input /></form>
525 * @desc Prevents the form submission when the input has no value entered.
529 * @param Function fn A function to bind to the submit event on each of the matched elements.
534 * Trigger the submit event of each matched element. This causes all of the functions
535 * that have been bound to thet submit event to be executed.
537 * Note: This does not execute the submit method of the form element! If you need to
538 * submit the form via code, you have to use the DOM method, eg. $("form")[0].submit();
540 * @example $("form").submit();
541 * @desc Triggers all submit events registered for forms, but does not submit the form
549 * Bind a function to the focus event of each matched element.
551 * @example $("p").focus( function() { alert("Hello"); } );
552 * @before <p>Hello</p>
553 * @result <p onfocus="alert('Hello');">Hello</p>
557 * @param Function fn A function to bind to the focus event on each of the matched elements.
562 * Trigger the focus event of each matched element. This causes all of the functions
563 * that have been bound to thet focus event to be executed.
565 * Note: This does not execute the focus method of the underlying elements! If you need to
566 * focus an element via code, you have to use the DOM method, eg. $("#myinput")[0].focus();
568 * @example $("p").focus();
569 * @before <p onfocus="alert('Hello');">Hello</p>
570 * @result alert('Hello');
578 * Bind a function to the keydown event of each matched element.
580 * @example $("p").keydown( function() { alert("Hello"); } );
581 * @before <p>Hello</p>
582 * @result <p onkeydown="alert('Hello');">Hello</p>
586 * @param Function fn A function to bind to the keydown event on each of the matched elements.
591 * Bind a function to the dblclick event of each matched element.
593 * @example $("p").dblclick( function() { alert("Hello"); } );
594 * @before <p>Hello</p>
595 * @result <p ondblclick="alert('Hello');">Hello</p>
599 * @param Function fn A function to bind to the dblclick event on each of the matched elements.
604 * Bind a function to the keypress event of each matched element.
606 * @example $("p").keypress( function() { alert("Hello"); } );
607 * @before <p>Hello</p>
608 * @result <p onkeypress="alert('Hello');">Hello</p>
612 * @param Function fn A function to bind to the keypress event on each of the matched elements.
617 * Bind a function to the error event of each matched element.
619 * @example $("p").error( function() { alert("Hello"); } );
620 * @before <p>Hello</p>
621 * @result <p onerror="alert('Hello');">Hello</p>
625 * @param Function fn A function to bind to the error event on each of the matched elements.
630 * Bind a function to the blur event of each matched element.
632 * @example $("p").blur( function() { alert("Hello"); } );
633 * @before <p>Hello</p>
634 * @result <p onblur="alert('Hello');">Hello</p>
638 * @param Function fn A function to bind to the blur event on each of the matched elements.
643 * Trigger the blur event of each matched element. This causes all of the functions
644 * that have been bound to thet blur event to be executed.
646 * Note: This does not execute the blur method of the underlying elements! If you need to
647 * blur an element via code, you have to use the DOM method, eg. $("#myinput")[0].blur();
649 * @example $("p").blur();
650 * @before <p onblur="alert('Hello');">Hello</p>
651 * @result alert('Hello');
659 * Bind a function to the load event of each matched element.
661 * @example $("p").load( function() { alert("Hello"); } );
662 * @before <p>Hello</p>
663 * @result <p onload="alert('Hello');">Hello</p>
667 * @param Function fn A function to bind to the load event on each of the matched elements.
672 * Bind a function to the select event of each matched element.
674 * @example $("p").select( function() { alert("Hello"); } );
675 * @before <p>Hello</p>
676 * @result <p onselect="alert('Hello');">Hello</p>
680 * @param Function fn A function to bind to the select event on each of the matched elements.
685 * Trigger the select event of each matched element. This causes all of the functions
686 * that have been bound to thet select event to be executed.
688 * @example $("p").select();
689 * @before <p onselect="alert('Hello');">Hello</p>
690 * @result alert('Hello');
698 * Bind a function to the mouseup event of each matched element.
700 * @example $("p").mouseup( function() { alert("Hello"); } );
701 * @before <p>Hello</p>
702 * @result <p onmouseup="alert('Hello');">Hello</p>
706 * @param Function fn A function to bind to the mouseup event on each of the matched elements.
711 * Bind a function to the unload event of each matched element.
713 * @example $("p").unload( function() { alert("Hello"); } );
714 * @before <p>Hello</p>
715 * @result <p onunload="alert('Hello');">Hello</p>
719 * @param Function fn A function to bind to the unload event on each of the matched elements.
724 * Bind a function to the change event of each matched element.
726 * @example $("p").change( function() { alert("Hello"); } );
727 * @before <p>Hello</p>
728 * @result <p onchange="alert('Hello');">Hello</p>
732 * @param Function fn A function to bind to the change event on each of the matched elements.
737 * Bind a function to the mouseout event of each matched element.
739 * @example $("p").mouseout( function() { alert("Hello"); } );
740 * @before <p>Hello</p>
741 * @result <p onmouseout="alert('Hello');">Hello</p>
745 * @param Function fn A function to bind to the mouseout event on each of the matched elements.
750 * Bind a function to the keyup event of each matched element.
752 * @example $("p").keyup( function() { alert("Hello"); } );
753 * @before <p>Hello</p>
754 * @result <p onkeyup="alert('Hello');">Hello</p>
758 * @param Function fn A function to bind to the keyup event on each of the matched elements.
763 * Bind a function to the click event of each matched element.
765 * @example $("p").click( function() { alert("Hello"); } );
766 * @before <p>Hello</p>
767 * @result <p onclick="alert('Hello');">Hello</p>
771 * @param Function fn A function to bind to the click event on each of the matched elements.
776 * Trigger the click event of each matched element. This causes all of the functions
777 * that have been bound to thet click event to be executed.
779 * @example $("p").click();
780 * @before <p onclick="alert('Hello');">Hello</p>
781 * @result alert('Hello');
789 * Bind a function to the resize event of each matched element.
791 * @example $("p").resize( function() { alert("Hello"); } );
792 * @before <p>Hello</p>
793 * @result <p onresize="alert('Hello');">Hello</p>
797 * @param Function fn A function to bind to the resize event on each of the matched elements.
802 * Bind a function to the mousemove event of each matched element.
804 * @example $("p").mousemove( function() { alert("Hello"); } );
805 * @before <p>Hello</p>
806 * @result <p onmousemove="alert('Hello');">Hello</p>
810 * @param Function fn A function to bind to the mousemove event on each of the matched elements.
815 * Bind a function to the mousedown event of each matched element.
817 * @example $("p").mousedown( function() { alert("Hello"); } );
818 * @before <p>Hello</p>
819 * @result <p onmousedown="alert('Hello');">Hello</p>
823 * @param Function fn A function to bind to the mousedown event on each of the matched elements.
828 * Bind a function to the mouseover event of each matched element.
830 * @example $("p").mouseover( function() { alert("Hello"); } );
831 * @before <p>Hello</p>
832 * @result <p onmouseover="alert('Hello');">Hello</p>
836 * @param Function fn A function to bind to the mousedown event on each of the matched elements.
839 jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
840 "mousedown,mouseup,mousemove,mouseover,mouseout,change,select," +
841 "submit,keydown,keypress,keyup,error").split(","), function(i,o){
843 // Handle event binding
844 jQuery.fn[o] = function(f){
845 return f ? this.bind(o, f) : this.trigger(o);
850 // If Mozilla is used
851 if ( jQuery.browser.mozilla || jQuery.browser.opera )
852 // Use the handy event callback
853 document.addEventListener( "DOMContentLoaded", jQuery.ready, false );
855 // If IE is used, use the excellent hack by Matthias Miller
856 // http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
857 else if ( jQuery.browser.msie ) {
859 // Only works if you document.write() it
860 document.write("<scr" + "ipt id=__ie_init defer=true " +
861 "src=//:><\/script>");
863 // Use the defer script hack
864 var script = document.getElementById("__ie_init");
866 // script does not exist if jQuery is loaded dynamically
868 script.onreadystatechange = function() {
869 if ( this.readyState != "complete" ) return;
870 this.parentNode.removeChild( this );
878 } else if ( jQuery.browser.safari )
879 // Continually check to see if the document.readyState is valid
880 jQuery.safariTimer = setInterval(function(){
881 // loaded and complete are both valid states
882 if ( document.readyState == "loaded" ||
883 document.readyState == "complete" ) {
885 // If either one are found, remove the timer
886 clearInterval( jQuery.safariTimer );
887 jQuery.safariTimer = null;
889 // and execute any waiting functions
894 // A fallback to window.onload, that will always work
895 jQuery.event.add( window, "load", jQuery.ready );
899 // Clean up after IE to avoid memory leaks
900 if (jQuery.browser.msie)
901 jQuery(window).one("unload", function() {
902 var global = jQuery.event.global;
903 for ( var type in global ) {
904 var els = global[type], i = els.length;
905 if ( i && type != 'unload' )
907 jQuery.event.remove(els[i-1], type);