开发者

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

This question already has answers here: 开发者_运维百科 Closed 11 years ago.

Possible Duplicate:

JQuery - multiple $(document).ready … ?

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


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.


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.


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.


All that function does is add an event handler to the onload event for the document, so by doing that you are simply making Javascript execute more than one thing upon finishing loading the page.

There are no downsides to it performance wise. However, if you're having all of that stuff happen upon pageload, you might want to have one init.js file (or something similar) that calls all of the other functions upon pageload. This way it's all in one place and easier to manage.

However, besdies readability and management, it's perfectly fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜