Multiple $(document).ready functions [duplicate]
If I have multiple $(document).ready(...)
functions, do they overwrite each other? For the sake of argument, pretend proper coding is thrown out the door on this one.
Say I have a $(document).ready(function() {...});
in my site's script file. Then I use a third party plugin that also uses $(document).ready(function() {...});
. Will this overwrite my already created function or does jQuery "queue" these functions to all run when the document is ready?
No, they do not override each other. Each function is executed.
You could of course check this easily yourself: http://jsfiddle.net/6jgGt/
Or understand from the jQuery code itself:
Line 255 is the ready function where the jQuery.bindReady();
is called which among other things initialises the readyList
object on line 429 with readyList = jQuery._Deferred();
And once it's a deferred object the function passed in is appended with readyList.done( fn );
and we can see in the done
method on line 41 that the element is added to an array with callbacks.push( elem );
so each one is saved separately...
No, they don't overwrite each other. They are queued, like you said.
精彩评论