0

I have a following function.

var sectionSize = {
    onReady: function ($) {
        var viewportHeight = $(window).height();
        $("#masthead").height(viewportHeight);
    }
}

There will be another function inside var sectionSize later. I want the function onReady to be called on document.ready. I learned that to do this, I have to use following code:

jQuery( document ).ready( sectionSize.onReady );

This works perfectly, doing what I need. But I already have a function document.ready there, which already contains another initializing function

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

I think to have document.ready twice in the file is nonsence but I am not sure, how to combine these two, so it both initialize the original code and do the sectionSize.onReady function. Anybody help? Thank you!

Very Curious
  • 881
  • 3
  • 26
  • 50
  • There's nothing wrong with having multiple document ready calls. Additionally, `(function () {})()` inside a document ready is redundant. The issues it tries to resolve are already resolved by the call to `jQuery.fn.ready`. – Kevin B Oct 20 '14 at 19:14
  • Check this. It's fine. http://stackoverflow.com/questions/5263385/jquery-multiple-document-ready – Joey Ezekiel Oct 20 '14 at 19:16
  • 1
    Alternately, if you want to combine the two, combine the two. I think it's easier to understand when it's all in one place. Call `sectionSize.onReady` and whatever you have inside the other one inside the same `jQuery( document ).ready();` – Surreal Dreams Oct 20 '14 at 19:18

1 Answers1

3

It's perfectly legal to register multiple jQuery(document).ready functions. They will be invoked in the order in which they were registered.

Alnitak
  • 334,560
  • 70
  • 407
  • 495