What happens when you have two jQuery $(document).ready calls in two JavaScript files used on the same HTML page?

前端 未结 5 1424
生来不讨喜
生来不讨喜 2020-12-15 03:50

I have a question on jQuery $(document).ready

Let\'s say we have a HTML page which includes 2 JavaScript files



        
5条回答
  •  萌比男神i
    2020-12-15 04:03

    See here: jQuery - is it bad to have multiple $(document).ready(function() {}); and here: Tutorials:Multiple $(document).ready()

    1. Yes
    2. Order of attach. jQuery internally maintains a readyList with Deferred objects.
    3. It's partially a matter of taste. Having one ready handler will give you a nice overview of all that is happening, while multiple (i.e., one per included file) will make your code much more modular (i.e., you can include or remove a .js file and be sure that it provides and binds its own ready handler).
    4. Yes - order of attach.

提交回复
热议问题