Trigger jQuery's $(window).resize only after $(window).load

Robin Cox picture Robin Cox · May 8, 2014 · Viewed 7.8k times · Source

I want something to happen when I resize my browser window, but every thing on the page must be loaded first. This doesn't work, but you get the picture:

$(window).resize(function(){
    $(window).load(function(){
        // do stuff
    });
});

How can I make this work?

Answer

Rory McCrossan picture Rory McCrossan · May 8, 2014

You need to attach the resize handler after the page has loaded, so reverse your event handlers:

$(window).load(function(){
    $(window).resize(function(){
        // do stuff
    });
});