-3

Possible Duplicate:
Can you have multiple $(document).ready(function(){ … }); sections?

May be this question is little bit funny.

I just want to know that can we use $(document).ready()more then one time on a page

If yes then what is the pros and cons for that?

Community
  • 1
  • 1
user1667633
  • 4,449
  • 2
  • 16
  • 21

2 Answers2

0

Yes you can call it as many times as you want, but it's not a good practice though. No cons as such, I think.

Pulkit Mittal
  • 5,916
  • 5
  • 21
  • 28
0

you can use it more than once. In fact, if you don't care at all about keeping your code small, you could litter your javascript file with them.

$(document).ready(function() {
      // some code here
    });
    $(document).ready(function() {
      // other code here
    });

It's great to be able to group your functions within a file or even across multiple files, and jQuery's flexible $(document).ready() function allows you to do that, pain free.

read more

Try this out:

$(document).ready(function() {
    alert('1!');
});

$(document).ready(function() {
    alert('2!');
});

$(document).ready(function() {
    alert('3!');
});

above code is the same as below

$(document).ready(function() {
    alert('1!');
    alert('2!');
    alert('3!');
});
Techie
  • 44,706
  • 42
  • 157
  • 243