Modified ready to event to pass jQuery object, allowing users to avoid the ugly custom alias pattern, added examples to both ready(Function) and $(Function)

This commit is contained in:
Jörn Zaefferer 2007-01-10 18:46:41 +00:00
parent 7b439921c4
commit c6e6e72561
2 changed files with 23 additions and 2 deletions

View file

@ -409,6 +409,10 @@ jQuery.fn.extend({
* will be called the instant the DOM is ready to be read and manipulated, * will be called the instant the DOM is ready to be read and manipulated,
* which is exactly what 99.99% of all Javascript code needs to run. * which is exactly what 99.99% of all Javascript code needs to run.
* *
* There is one argument passed to the ready event handler: A reference to
* the jQuery function. You can name that argument whatever you like, and
* can therefore stick with the $ alias without risc of naming collisions.
*
* Please ensure you have no code in your <body> onload event handler, * Please ensure you have no code in your <body> onload event handler,
* otherwise $(document).ready() may not fire. * otherwise $(document).ready() may not fire.
* *
@ -417,21 +421,30 @@ jQuery.fn.extend({
* *
* @example $(document).ready(function(){ Your code here... }); * @example $(document).ready(function(){ Your code here... });
* *
* @example jQuery(function($) {
* // Your code using failsafe $ alias here...
* });
* @desc Uses both the shortcut for $(document).ready() and the argument
* to write failsafe jQuery code using the $ alias, without relying on the
* global alias.
*
* @name ready * @name ready
* @type jQuery * @type jQuery
* @param Function fn The function to be executed when the DOM is ready. * @param Function fn The function to be executed when the DOM is ready.
* @cat Events * @cat Events
* @see $.noConflict()
* @see $(Function)
*/ */
ready: function(f) { ready: function(f) {
// If the DOM is already ready // If the DOM is already ready
if ( jQuery.isReady ) if ( jQuery.isReady )
// Execute the function immediately // Execute the function immediately
f.apply( document ); f.apply( document, [jQuery] );
// Otherwise, remember the function for later // Otherwise, remember the function for later
else { else {
// Add the function to the wait list // Add the function to the wait list
jQuery.readyList.push( f ); jQuery.readyList.push( function() { return f.apply(this, [jQuery]) } );
} }
return this; return this;

View file

@ -154,10 +154,18 @@ var $ = jQuery;
* }); * });
* @desc Executes the function when the DOM is ready to be used. * @desc Executes the function when the DOM is ready to be used.
* *
* @example jQuery(function($) {
* // Your code using failsafe $ alias here...
* });
* @desc Uses both the shortcut for $(document).ready() and the argument
* to write failsafe jQuery code using the $ alias, without relying on the
* global alias.
*
* @name $ * @name $
* @param Function fn The function to execute when the DOM is ready. * @param Function fn The function to execute when the DOM is ready.
* @cat Core * @cat Core
* @type jQuery * @type jQuery
* @see ready(Function)
*/ */
jQuery.fn = jQuery.prototype = { jQuery.fn = jQuery.prototype = {