Search code examples
jqueryonload-event

jQuery resize function doesn't work on page load


How do I get this function to not only run on window resize but also on initial page load?

$(window).resize(function() {
...  
});

Solution

  • This solution is now deprecated since jQuery 3.0: https://api.jquery.com/bind/#bind-eventType-eventData-handler

    You'll want to use:

    $(document).ready(function() { /* your code */ });
    

    To make something happen onload. If you want something to work onload and onresize, you should do:

    onResize = function() { /* your code */ }
    
    $(document).ready(onResize);
    
    $(window).bind('resize', onResize);