Alex Rivera | Logout

Jquery - More than 1 "$(document).ready" = dirty code?

Asked 2010-09-17T08:09:44.960
9

is it OK to use the

$(document).ready(function ()
{   

// some code

});

more than 1 time in the javascript code?

Edit
Report

1 Answer

1

I find that having everything in one huge $(document).ready leads to messy code thats hard to read.

I often prefer to split it up and place a separate $(document).ready() for every part of the system that needs stuff added to it. This is especially nice for larger, modular, systems, where you dynamically add blocks of html, events and stuff.

For me it all boils down to what is most beneficial for you as a developer in a certain case.

  • Small system, where it's easy to know what's going on: one $(document).ready() in your script.
  • Large, modular, system: split it up as needed to have control of what's going on, and develop efficiently.

But as @Codesleuth commented: very often you don't need to put stuff inside the $(document).ready(), you only need it when you need to be sure the DOM is in a consistant and known state for heavy manipulation etc.

answered 2010-09-17T08:30:56.710

Your Answer