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?

Spidy
  • 39,723
  • 15
  • 65
  • 83
  • 1
    No, just keep this in mind if you're using it a lot: http://encosia.com/dont-let-jquerys-document-ready-slow-you-down/ – Paul Jul 21 '11 at 00:37
  • @PaulPRO - Great link. I never thought about that. – Spidy Jul 21 '11 at 00: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. – Alastair Pitts Jul 21 '11 at 01:03
  • @Alastair - I did a search and looked at the suggested questions. I always do. Not my fault stackoverflow didn't show them. – Spidy Jul 21 '11 at 01:18

2 Answers2

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

davin
  • 44,863
  • 9
  • 78
  • 78
  • 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 Jul 02 '18 at 12:29
  • @Panomosh The latter. – endo64 Jan 23 '20 at 14:57
4

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

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91