Jquery document.ready multiple declarations

I realized that I can specify $(document).ready(function(){}); more than once.
Suppose it is

 $(document).ready(function(){ var abc = "1122"; //do something.. }); $(document).ready(function(){ var abc = "def"; //do something.. }); 
  • Is this a standard? These codes work on my FF (16.0.2). I'm just a little afraid that another browser may not.
  • What is really going on? How does jQuery handle this code?

Thanks.

+4
source share
3 answers

Yes, it is standard and recommended, and jQuery guarantees that these functions will be executed in the order of declaration (see this related answer for details on implementation).

The variable abc is local to your functions and does not interfere. You cannot see the abc value declared in one of the callbacks from the other.

+10
source

Yes, you can use several document.ready functions on the same page. It will not give any error or exception in any browser. and the function will be executed for upper and lower order.

+1
source

Although it is standard, I would prefer to use only one $ (document.ready) file ... so that it is DRY, otherwise you can end up saying 10 of these ads on your page, which will make it look unmaintanable spaghetti code (but JS allows you to do this too)

0
source

All Articles