I have an aspx page with 5 user controls. I have $(document).ready()
in ea开发者_如何学运维ch of these user controls and then one in the main aspx page.
Is this the right approach or I should consider of side effects?
It is fine, you just cannot guarantee the order in which they functions will be fired.
Being as that reducing the amount of $(document).ready()
calls is subjective to the code itself, whether or not it is the correct approach for you to take is hard to say without seeing the code/design.
If the controls are independent of each-other and independent of the page, YES it is OK to run $(document).ready()
multiple times.
This is totally fine unless you need to run them in a specific order.
Shortcut (works the same way):
$(function() {
});
Either combine them or don't, there are no side-effects just a little extra script running. Whichever is easier to manage is ok, if you have more than a few controls I'd really look at combining the output somehow.
精彩评论