Having multiple ready()
blocks on the one page is fine. In fact, it's exactly set up so you can do this. The old method (usually used on load()
events) did things like:
window.onload = function() {
...
}
but that quickly broke when multiple scripts both assigned load()
handlers, which led to:
function addLoad(handler) {
var current = window.onload;
if (current) {
window.onload = function() {
current();
handler();
}
} else {
window.onload = handler;
}
}
Well, there was a little more to it than that but that was the basic principle. jQuery essentially takes care of that boilerplate for you so your event handlers don't clobber each other.
Multiple ready()
handlers makes your code easier to maintain and probably easier to read. If you're only doing a few then don't even worry about it. If you get up to hundreds (on the same page) then maybe it's time to rethink.
The alternate approach is to write your markup to a buffer in PHP and all your ready()
code to another buffer and at the end you can write out those buffers in whatever order you like. You could effectively combine your ready()
handlers into one this way.