Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implications of having multiple $(document).ready functions across multiple js files [duplicate]

Possible Duplicate:
JQuery - multiple $(document).ready … ?

What are the implications of having multiple javascript files, each with its own $(document).ready function?

like image 867
Bart Jedrocha Avatar asked May 03 '11 18:05

Bart Jedrocha


3 Answers

When having multiple things bind with the $(document).ready();, the events will be hit in the order that they were bound. There are no real negative repercussions with this, but keep in mind that any variables defined in one of these methods loses scope, and won't be available in the other events.

To get around the javascript scoping issues, just define cross-ready-event variables outside of the $(document).ready(); blocks.

like image 96
Mike Trpcic Avatar answered Oct 19 '22 06:10

Mike Trpcic


All functions will be executed in the order they were bound. See the jQuery documentation:

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. After all handlers have executed, the event continues along the normal event propagation path.

(http://api.jquery.com/bind/)

So all functions are executed and you can influence the order in which they are executed. However, I would strongly advice against basing any logic in the execution order as this may proof pretty brittle.

like image 43
Peter Avatar answered Oct 19 '22 07:10

Peter


nothing. They all will attach a lister to be executed when the document is ready. Only implication afaik is that they will be executed in the order they were attached.

like image 28
Crayon Violent Avatar answered Oct 19 '22 05:10

Crayon Violent