Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple $(document).ready functions [duplicate]

Tags:

html

jquery

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?

like image 217
Spidy Avatar asked Jul 21 '11 00:07

Spidy


1 Answers

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

like image 90
davin Avatar answered Oct 29 '22 13:10

davin