开发者

Can running 2 document.ready make them conflict?

In my application i am running $(document).ready( twice on on the same page is there going to be a conflict betwe开发者_运维问答en them?

Thanks in Advance,

Dean


Nope. jQuery events are stacked upon each other and will be executed one by one.

From the jQuery docs on bind():

When an event reaches an element, all handlers bound to that event type for the element are fired. If there are multiple handlers registered, they will always execute in the order in which they were bound.


There won't be a conflict.

$(document).ready(function() { alert("One"); });
$(document).ready(function() { alert("Two"); });

Will alert twice.


$(document).ready()

Is very safe--you can hook multiple handlers to it or use it long after the event has already fired.

If you debug into jQuery (just include an unminified version) you'll see that .ready() looks like:

ready: function( fn ) {
    // Attach the listeners
    jQuery.bindReady();

    // If the DOM is already ready
    if ( jQuery.isReady ) {
        // Execute the function immediately
        fn.call( document, jQuery );

    // Otherwise, remember the function for later
    } else if ( readyList ) {
        // Add the function to the wait list
        readyList.push( fn );
    }

    return this;
}

So, if jQuery.isReady (indicating that document-ready has already occured) then the $(document).ready() function gets called immediately. Otherwise, the function is added to an array of handlers.

Inside jQuery's own ready handler:

        // If there are functions bound, to execute
        if ( readyList ) {
            // Execute all of them
            var fn, i = 0;
            while ( (fn = readyList[ i++ ]) ) {
                fn.call( document, jQuery );
            }

            // Reset the list of functions
            readyList = null;
        }

So, on ready jQuery loops through and calls all the functions which were bound to ready. These functions are called in serial (one after another, not in parallel). This means that one will finish before the next will start.


No. jQuery is designed to stack them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜