Search code examples
jqueryajaxdocumentdocument-readywindow-load

Wrapping $(document).ready inside $(window).load?


Based on the answer here, I need to wrap all of my code in $(window).load(function(){. However, my gallery is also using imagesLoaded (to load images when they are ready) and ajax to load more items on a button click. My current layout is like this:

function initialise(){
//code goes here, including imagesLoaded
};

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

$(document).ajaxComplete(function () {
    initialise();

//ajax code goes here
}); 

How can I wrap all of this in $(window).load(function () {?


Solution

  • Just like this!

    $(window).load(function () {
        function initialise(){
        //code goes here, including imagesLoaded
        };
    
        $(document).ready(function(){
            initialise();
        });
    
        $(document).ajaxComplete(function () {
            initialise();
    
        //ajax code goes here
        });
    });