jQuery: The Write Less, Do More JavaScript Library

Changeset 4211

Show
Ignore:
Timestamp:
12/17/07 20:22:53 (8 months ago)
Author:
brandon.aaron
Message:

Fixed #2069. The ready helper and shortcuts act the same. You can also still bind, unbind and trigger the ready event on the document element but doing so follows the events API unlike the ready helper method.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/jquery/src/event.js

    r4191 r4211  
    135135                        for ( ret in events[type] ) break; 
    136136                        if ( !ret ) { 
    137                             if ( !jQuery.event.special[type] || jQuery.event.special[type].teardown.call(this, elem) === false ) { 
     137                            if ( !jQuery.event.special[type] || jQuery.event.special[type].teardown.call(elem) === false ) { 
    138138                                if (elem.removeEventListener) 
    139139                                    elem.removeEventListener(type, jQuery.data(elem, "handle"), false); 
     
    324324        ready: { 
    325325            setup: function() { 
    326                 var handler = jQuery.event.special.ready.handler; 
    327                  
    328                 // Mozilla, Opera and webkit nightlies currently support this event 
    329                 if ( document.addEventListener ) 
    330                     // Use the handy event callback 
    331                     document.addEventListener( "DOMContentLoaded", handler, false ); 
    332      
    333                 // If Safari or IE is used 
    334                 // Continually check to see if the document is ready 
    335                 if ((jQuery.browser.msie && window == top) || jQuery.browser.safari ) (function(){ 
    336                     try { 
    337                         // If IE is used, use the trick by Diego Perini 
    338                         // http://javascript.nwbox.com/IEContentLoaded/ 
    339                         if ( jQuery.browser.msie || document.readyState != "loaded" && document.readyState != "complete" ) 
    340                             document.documentElement.doScroll("left"); 
    341                     } catch( error ) { 
    342                         setTimeout( arguments.callee, 0 ); 
    343                         return; 
    344                     } 
    345  
    346                     // and execute any waiting functions 
    347                     handler(); 
    348                 })(); 
    349  
    350                 // A fallback to window.onload, that will always work 
    351                 jQuery.event.add( window, "load", handler ); 
     326                // Make sure the ready event is setup 
     327                bindReady(); 
     328                return; 
    352329            }, 
    353330             
    354             teardown: function() {return;}, 
    355              
    356             handler: function() { 
    357                 // Make sure that the DOM is not already loaded 
    358                 if ( !jQuery.isReady ) { 
    359                     // Remember that the DOM is ready 
    360                     jQuery.isReady = true; 
    361                     jQuery(document).triggerHandler("ready"); 
    362                     jQuery(document).unbind("ready"); 
    363                 } 
    364             } 
     331            teardown: function() { return; } 
    365332        }, 
    366333         
     
    463430    hover: function(fnOver, fnOut) { 
    464431        return this.bind('mouseenter', fnOver).bind('mouseleave', fnOut); 
     432    }, 
     433     
     434    ready: function(fn) { 
     435        // Attach the listeners 
     436        bindReady(); 
     437 
     438        // If the DOM is already ready 
     439        if ( jQuery.isReady ) 
     440            // Execute the function immediately 
     441            fn.call( document, jQuery ); 
     442             
     443        // Otherwise, remember the function for later 
     444        else 
     445            // Add the function to the wait list 
     446            jQuery.readyList.push( function() { return fn.call(this, jQuery); } ); 
     447     
     448        return this; 
    465449    } 
    466450}); 
    467451 
    468452jQuery.extend({ 
    469     isReady: false 
     453    isReady: false, 
     454    readyList: [], 
     455    // Handle when the DOM is ready 
     456    ready: function() { 
     457        // Make sure that the DOM is not already loaded 
     458        if ( !jQuery.isReady ) { 
     459            // Remember that the DOM is ready 
     460            jQuery.isReady = true; 
     461             
     462            // If there are functions bound, to execute 
     463            if ( jQuery.readyList ) { 
     464                // Execute all of them 
     465                jQuery.each( jQuery.readyList, function(){ 
     466                    this.apply( document ); 
     467                }); 
     468                 
     469                // Reset the list of functions 
     470                jQuery.readyList = null; 
     471            } 
     472         
     473            // Trigger any bound ready events 
     474            $(document).triggerHandler("ready"); 
     475        } 
     476    } 
    470477}); 
    471478 
    472 jQuery.each( ("blur,focus,load,ready,resize,scroll,unload,click,dblclick," + 
     479var readyBound = false; 
     480 
     481function bindReady(){ 
     482    if ( readyBound ) return; 
     483    readyBound = true; 
     484 
     485    // Mozilla, Opera and webkit nightlies currently support this event 
     486    if ( document.addEventListener ) 
     487        // Use the handy event callback 
     488        document.addEventListener( "DOMContentLoaded", jQuery.ready, false ); 
     489     
     490    // If Safari or IE is used 
     491    // Continually check to see if the document is ready 
     492    if (jQuery.browser.msie || jQuery.browser.safari ) (function(){ 
     493        try { 
     494            // If IE is used, use the trick by Diego Perini 
     495            // http://javascript.nwbox.com/IEContentLoaded/ 
     496            if ( jQuery.browser.msie || document.readyState != "loaded" && document.readyState != "complete" ) 
     497                document.documentElement.doScroll("left"); 
     498        } catch( error ) { 
     499            return setTimeout( arguments.callee, 0 ); 
     500        } 
     501 
     502        // and execute any waiting functions 
     503        jQuery.ready(); 
     504    })(); 
     505 
     506    // A fallback to window.onload, that will always work 
     507    jQuery.event.add( window, "load", jQuery.ready ); 
     508} 
     509 
     510jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," + 
    473511    "mousedown,mouseup,mousemove,mouseover,mouseout,change,select," +  
    474512    "submit,keydown,keypress,keyup,error").split(","), function(i, name){