jQuery resize function doesn't work on page load

Nicki picture Nicki · Apr 8, 2010 · Viewed 23.7k times · Source

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

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

Answer

Kristopher picture Kristopher · Apr 8, 2010

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);