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

前端 未结 11 1222
庸人自扰
庸人自扰 2020-11-22 13:21

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 such

相关标签:
11条回答
  • 2020-11-22 13:28

    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.

    0 讨论(0)
  • 2020-11-22 13:30

    I think the better way to go is to put switch to named functions (Check this overflow for more on that subject). That way you can call them from a single event.

    Like so:

    function firstFunction() {
        console.log("first");
    }
    
    function secondFunction() {
        console.log("second");
    }
    
    
    function thirdFunction() {
        console.log("third");
    }
    

    That way you can load them in a single ready function.

    jQuery(document).on('ready', function(){
       firstFunction();
       secondFunction();
       thirdFunction();
    

    });

    This will output the following to your console.log:

    first
    second
    third
    

    This way you can reuse the functions for other events.

    jQuery(window).on('resize',function(){
        secondFunction();
    });
    

    Check this fiddle for working version

    0 讨论(0)
  • 2020-11-22 13:35

    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!');
    });
    
    0 讨论(0)
  • 2020-11-22 13:35

    It's legal, but sometimes it cause undesired behaviour. As an Example I used the MagicSuggest library and added two MagicSuggest inputs in a page of my project and used seperate document ready functions for each initializations of inputs. The very first Input initialization worked, but not the second one and also not giving any error, Second Input didn't show up. So, I always recommend to use one Document Ready Function.

    0 讨论(0)
  • 2020-11-22 13:37

    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. (source)

    0 讨论(0)
  • 2020-11-22 13:41

    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
    
    0 讨论(0)
提交回复
热议问题