9

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?

4
  • 1
    No, just keep this in mind if you're using it a lot: encosia.com/dont-let-jquerys-document-ready-slow-you-down Commented Jul 21, 2011 at 0:37
  • @PaulPRO - Great link. I never thought about that. Commented Jul 21, 2011 at 0:43
  • @Spidy: did you even look a the suggested questions when creating this question? There is a ton of these questions already. It's a direct duplicate of about 5 other questions. Commented Jul 21, 2011 at 1:03
  • @Alastair - I did a search and looked at the suggested questions. I always do. Not my fault stackoverflow didn't show them. Commented Jul 21, 2011 at 1:18

2 Answers 2

19

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...

Sign up to request clarification or add additional context in comments.

2 Comments

sorry for resurrecting this, but, are the two document ready calls hoisted up into one call when it is interpreted by the browser, or are they literally queued and called one after the other?
@Panomosh The latter.
4

No, they don't overwrite each other. They are queued, like you said.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.