tags:

views:

1151

answers:

8

If i have a lot of functions on startup do they all have to be under one single:

$(document).ready(function() {

or can i have multiple:

$(document).ready(function() {

statements?

+19  A: 

You can have multiple ones, but it's not always the neatest thing to do. Try not to overuse them, as it will seriously affect readability. Other than that , it's perfectly legal. See the below:

http://www.learningjquery.com/2006/09/multiple-document-ready

Try this out:

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

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

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

You'll find that it's equivalent to this, note the order of execution:

$(document).ready(function() {
    alert('Hello Tom!');
    alert('Hello Jeff!');
    alert('Hello Dexter!');
});

It's also worth noting that a function defined within one $(document).ready block cannot be called from another $(document).ready block, I just ran this test:

$(document).ready(function() {
    alert('hello1');
    function saySomething() {
        alert('something');
    }
    saySomething();

});
$(document).ready(function() {
    alert('hello2');
    saySomething();
});

output was:

hello1
something
hello2
karim79
It's not wise to rely on the order of execution when using multiple $(document).ready() statements. Each one needs to execute independently of any other having already been executed or not.
James
@AoP - I don't know how much sense that makes, if they are not executed in order, then they are completely meaningless within the context of the application, so using multiple $(document).ready( blocks would be broken altogether.
karim79
+4  A: 

As written here, on jQuery's wiki: yes it is possible to have multiple $(document).ready() calls.

However, I don't think you can know in which way they will be executed.

JW.
called in the order added
redsquare
+6  A: 

Yes you can easily have multiple blocks. Just be careful with dependencies between them as the evaluation order might not be what you expect.

pjesi
A: 

It is no problem to have multiple document.ready sections. I use this myself quite often.

Ronald Wildenberg
+7  A: 

You can use multiple. But you can also use multiple functions inside one document.ready as well:

$(document).ready(function() {
    // Jquery
    $('.hide').hide();
    $('.test').each(function() {
       $(this).fadeIn();
    });

    // Reqular JS
    function test(word) {
       alert(word);
    }
    test('hello!');
});
Mickel
This is ok if you have full control of the code attached to the page. If someone has already written this in another file attached to the page, then you're ok to declare it like this.
James Wiseman
Not sure what you are trying to say. But if you put this inside your head, and include 15 other external JS, all wich contains one (or multiple document.load) it will still work as if there was just one. Worth to mention is that the scoop of each function/variable ends with the closing bracket of the $(document).ready-function.
Mickel
Just trying to say that sometimes, other folk will have written modules attached to the page that have a document ready function already declared, so you mightn't have the luxury of moving all your code into one function (which, granted, would be ideal). I would also be wary of having too much stuff here however, as we'll get On Ready Bloating, which can make the code equally as hard to maintain.
James Wiseman
Ah, now I get you. Yeah I agree on that. Just stated an example to prove that it's possible ;)
Mickel
A: 

Yes you can.

Multiple document ready sections are particularly useful if you have other modules haging off the same page that use it. With the old window.onload=func declaration, every time you specified a function to be called, it replaced the old.

Now all functions specified are queued/stacked (can someone confirm?) regardless of which document ready section they are specified in.

James Wiseman
Yes, it's true, the functions are queued up rater than replacing previous $(document).ready functions.
Deeksy
+1  A: 

Yes it is possible but you can better use a div #mydiv and use both

$(document).ready(function(){});

//and

$("#mydiv").ready(function(){});
Time Machine
+1  A: 

Yes, it's perfectly ok.but avoid doing it without a reason. For example I used it to declare global site rules seperately than indivual pages when my javascript files were generated dynamically but if you just keep doing it over and over it will make it hard to read.

Also you can not access some methods from another jQuery(function(){}); call so that's another reason you don't wanna do that.

With the old window.onload though you will replace the old one every time you specified a function.

Neo