Why is jQuery.ready recommended when it’s so slow?
CommunityDavid提出了一个问题：Why is jQuery.ready recommended when it’s so slow?，或许与您遇到的问题类似。
To the point first:
No, there is no disadvantage in calling you
init before closing the
<body>. It will as you have noticed perform better that relying on
$.ready() and will also work with all the browsers flawlessly (even on IE).
Now, there are however reasons to use
$.ready(), which in your case they do not probably apply:
$.ready()makes it easy for developers to do stuff in the right order. In particular, the critical thing is to not reference DOM elements that have not been loaded. While this is simple enough, lots of developers still find it confusing.
$.ready()is a no-brainer, albeit a slow one.
- In you have say several scripts that need to
init(), it is not necessarily easy/convenient to manually do that at the end of your body. It requires discipline and knowledge of what these scripts do. In particular you will often see
$.ready()in libraries dependent on jQuery, since it makes things work no matter what way developers will use to load the libs.
<body/>method is not guaranteed.