X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fevent%2Fevent.js;h=852b3135233eb017011376ef2060d0376eeeae84;hb=8937e088b6f06a0dbef36348aef5d7c10f3af340;hp=eb5d731b6a6da783da2dd3350241cf97f9e8ffd3;hpb=00e6e81f94a1831ceef76b371f9c186b4097d91e;p=jquery.git diff --git a/src/event/event.js b/src/event/event.js index eb5d731..852b313 100644 --- a/src/event/event.js +++ b/src/event/event.js @@ -10,30 +10,32 @@ jQuery.fn.extend({ * is fired, when clicked again, the second is fired. All subsequent * clicks continue to rotate through the two functions. * + * Use unbind("click") to remove. + * * @example $("p").toggle(function(){ * $(this).addClass("selected"); * },function(){ * $(this).removeClass("selected"); * }); - * + * * @name toggle * @type jQuery * @param Function even The function to execute on every even click. * @param Function odd The function to execute on every odd click. * @cat Events */ - toggle: function(a,b) { - // If two functions are passed in, we're - // toggling on a click - return a && b && a.constructor == Function && b.constructor == Function ? this.click(function(e){ + toggle: function() { + // save reference to arguments for access in closure + var a = arguments; + return typeof a[0] == "function" && typeof a[1] == "function" ? this.click(function(e) { // Figure out which function to execute - this.last = this.last == a ? b : a; + this.lastToggle = this.lastToggle == 0 ? 1 : 0; // Make sure that clicks stop e.preventDefault(); // and execute the function - return this.last.apply( this, [e] ) || false; + return a[this.lastToggle].apply( this, [e] ) || false; }) : // Otherwise, execute the old toggle function @@ -64,7 +66,7 @@ jQuery.fn.extend({ * @param Function over The function to fire whenever the mouse is moved over a matched element. * @param Function out The function to fire whenever the mouse is moved off of a matched element. * @cat Events - */ + */ hover: function(f,g) { // A private function for haandling mouse 'hovering' @@ -73,7 +75,7 @@ jQuery.fn.extend({ var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget; // Traverse up the tree - while ( p && p != this ) p = p.parentNode; + while ( p && p != this ) try { p = p.parentNode } catch(e) { p = this; }; // If we actually just moused on to a sub-element, ignore it if ( p == this ) return false; @@ -100,6 +102,9 @@ jQuery.fn.extend({ * Please ensure you have no code in your <body> onload event handler, * otherwise $(document).ready() may not fire. * + * You can have as many $(document).ready events on your page as you like. + * The functions are then executed in the order they were added. + * * @example $(document).ready(function(){ Your code here... }); * * @name ready @@ -146,6 +151,9 @@ jQuery.extend({ // Reset the list of functions jQuery.readyList = null; } + // Remove event lisenter to avoid memory leak + if ( jQuery.browser.mozilla || jQuery.browser.opera ) + document.removeEventListener( "DOMContentLoaded", jQuery.ready, false ); } } }); @@ -223,9 +231,11 @@ new function(){ /** * Bind a function to the submit event of each matched element. * - * @example $("p").submit( function() { alert("Hello"); } ); - * @before

Hello

- * @result

Hello

+ * @example $("#myform").submit( function() { + * return $("input", this).val().length > 0; + * } ); + * @before
+ * @desc Prevents the form submission when the input has no value entered. * * @name submit * @type jQuery @@ -237,9 +247,11 @@ new function(){ * Trigger the submit event of each matched element. This causes all of the functions * that have been bound to thet submit event to be executed. * - * @example $("p").submit(); - * @before

Hello

- * @result alert('Hello'); + * Note: This does not execute the submit method of the form element! If you need to + * submit the form via code, you have to use the DOM method, eg. $("form")[0].submit(); + * + * @example $("form").submit(); + * @desc Triggers all submit events registered for forms, but does not submit the form * * @name submit * @type jQuery @@ -305,6 +317,9 @@ new function(){ * Trigger the focus event of each matched element. This causes all of the functions * that have been bound to thet focus event to be executed. * + * Note: This does not execute the focus method of the underlying elements! If you need to + * focus an element via code, you have to use the DOM method, eg. $("#myinput")[0].focus(); + * * @example $("p").focus(); * @before

Hello

* @result alert('Hello'); @@ -645,6 +660,9 @@ new function(){ * Trigger the blur event of each matched element. This causes all of the functions * that have been bound to thet blur event to be executed. * + * Note: This does not execute the blur method of the underlying elements! If you need to + * blur an element via code, you have to use the DOM method, eg. $("#myinput")[0].blur(); + * * @example $("p").blur(); * @before

Hello

* @result alert('Hello'); @@ -713,11 +731,15 @@ new function(){ * Trigger the load event of each matched element. This causes all of the functions * that have been bound to thet load event to be executed. * + * Marked as private: Calling load() without arguments throws exception because the ajax load + * does not handle it. + * * @example $("p").load(); * @before

Hello

* @result alert('Hello'); * * @name load + * @private * @type jQuery * @cat Events/Browser */ @@ -1444,50 +1466,76 @@ new function(){ * @cat Events/Mouse */ - /** - * @test var count; - * var e = ("blur,focus,load,resize,scroll,unload,click,dblclick," + - * "mousedown,mouseup,mousemove,mouseover,mouseout,change,reset,select," + - * "submit,keydown,keypress,keyup,error").split(","); - * var handler1 = function(event) { - * count++; - * }; - * var handler2 = function(event) { - * count++; - * }; - * for( var i=0; i < e.length; i++) { - * var event = e[i]; - * count = 0; - * // bind handler - * $(document)[event](handler1); - * $(document)[event](handler2); - * $(document)["one"+event](handler1); - * - * // call event two times - * $(document)[event](); - * $(document)[event](); - * - * // unbind events - * $(document)["un"+event](handler1); - * // call once more - * $(document)[event](); - * - * // remove all handlers - * $(document)["un"+event](); - * - * // call once more - * $(document)[event](); - * - * // assert count - * @test ok( count == 6, 'Checking event ' + event); - * } - * - * @private - * @name eventTesting - */ + /** + * Bind a function to the mouseover event of each matched element. + * + * @example $("p").mouseover( function() { alert("Hello"); } ); + * @before

Hello

+ * @result

Hello

+ * + * @name mouseover + * @type jQuery + * @param Function fn A function to bind to the mousedown event on each of the matched elements. + * @cat Events/Mouse + */ + + /** + * Trigger the mouseover event of each matched element. This causes all of the functions + * that have been bound to thet mousedown event to be executed. + * + * @example $("p").mouseover(); + * @before

Hello

+ * @result alert('Hello'); + * + * @name mouseover + * @type jQuery + * @cat Events/Mouse + */ + + /** + * Bind a function to the mouseover event of each matched element, which will only be executed once. + * Unlike a call to the normal .mouseover() method, calling .onemouseover() causes the bound function to be + * only executed the first time it is triggered, and never again (unless it is re-bound). + * + * @example $("p").onemouseover( function() { alert("Hello"); } ); + * @before

Hello

+ * @result alert('Hello'); // Only executed for the first mouseover + * + * @name onemouseover + * @type jQuery + * @param Function fn A function to bind to the mouseover event on each of the matched elements. + * @cat Events/Mouse + */ + + /** + * Removes a bound mouseover event from each of the matched + * elements. You must pass the identical function that was used in the original + * bind method. + * + * @example $("p").unmouseover( myFunction ); + * @before

Hello

+ * @result

Hello

+ * + * @name unmouseover + * @type jQuery + * @param Function fn A function to unbind from the mouseover event on each of the matched elements. + * @cat Events/Mouse + */ + + /** + * Removes all bound mouseover events from each of the matched elements. + * + * @example $("p").unmouseover(); + * @before

Hello

+ * @result

Hello

+ * + * @name unmouseover + * @type jQuery + * @cat Events/Mouse + */ var e = ("blur,focus,load,resize,scroll,unload,click,dblclick," + - "mousedown,mouseup,mousemove,mouseover,mouseout,change,reset,select," + + "mousedown,mouseup,mousemove,mouseover,mouseout,change,select," + "submit,keydown,keypress,keyup,error").split(","); // Go through all the event names, but make sure that @@ -1506,20 +1554,16 @@ new function(){ // Finally, handle events that only fire once jQuery.fn["one"+o] = function(f){ - // Attach the event listener - return this.each(function(){ - - var count = 0; - - // Add the event - jQuery.event.add( this, o, function(e){ - // If this function has already been executed, stop - if ( count++ ) return; - - // And execute the bound function - return f.apply(this, [e]); - }); - }); + // save cloned reference to this + var element = jQuery(this); + var handler = function() { + // unbind itself when executed + element.unbind(o, handler); + element = null; + // apply original handler with the same arguments + return f.apply(this, arguments); + }; + return this.bind(o, handler); }; }; @@ -1539,11 +1583,12 @@ new function(){ // Use the defer script hack var script = document.getElementById("__ie_init"); - script.onreadystatechange = function() { - if ( this.readyState != "complete" ) return; - this.parentNode.removeChild( this ); - jQuery.ready(); - }; + if (script) // script does not exist if jQuery is loaded dynamically + script.onreadystatechange = function() { + if ( this.readyState != "complete" ) return; + this.parentNode.removeChild( this ); + jQuery.ready(); + }; // Clear from memory script = null; @@ -1570,3 +1615,12 @@ new function(){ jQuery.event.add( window, "load", jQuery.ready ); }; + +// Clean up after IE to avoid memory leaks +if (jQuery.browser.msie) jQuery(window).unload(function() { + var event = jQuery.event, global = event.global; + for (var type in global) { + var els = global[type], i = els.length; + if (i>0) do if (type != 'unload') event.remove(els[i-1], type); while (--i); + } +});