X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fevent%2Fevent.js;h=9f903212597aa2a687d8a438ab101f6508a0a762;hb=3e3240f7ed52c035f3f68d50836f089bd2d03409;hp=ff1d936fd70bddbc5b19abbf2b9237cf506a9cb6;hpb=394334671cd4a0b22c01f724b30933fc83df863d;p=jquery.git diff --git a/src/event/event.js b/src/event/event.js index ff1d936..9f90321 100644 --- a/src/event/event.js +++ b/src/event/event.js @@ -16,13 +16,6 @@ jQuery.fn.extend({ * $(this).removeClass("selected"); * }); * - * @test var count = 0; - * var fn1 = function() { count++; } - * var fn2 = function() { count--; } - * var link = $('#mark'); - * link.click().toggle(fn1, fn2).click().click().click().click().click(); - * ok( count == 1, "Check for toggle(fn, fn)" ); - * * @name toggle * @type jQuery * @param Function even The function to execute on every even click. @@ -80,7 +73,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; @@ -107,6 +100,8 @@ 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. + * * @example $(document).ready(function(){ Your code here... }); * * @name ready @@ -153,6 +148,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 ); } } }); @@ -230,9 +228,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 @@ -244,9 +244,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 @@ -720,11 +722,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 */ @@ -1518,49 +1524,6 @@ new function(){ * @type jQuery * @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 - * @cat Events - */ var e = ("blur,focus,load,resize,scroll,unload,click,dblclick," + "mousedown,mouseup,mousemove,mouseover,mouseout,change,reset,select," + @@ -1582,20 +1545,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); }; }; @@ -1615,11 +1574,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; @@ -1646,3 +1606,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); + } +});