views:

353

answers:

2
        $('.content .right').width($(window).width() - (480));
        $(window).resize(function(e) {
            $('.content .right').width($(window).width() - (480));
        });

Is there a better way to rewrite this more compactly? I want to call the function onload as well as on resize.

Thanks!

+2  A: 

You can use $.bind() to attach logic to various events simultaneously:

$(window).bind("load resize", function(){
  // code here
});

Although I'm pretty sure load is called by the document rather than the window. If that is the case, you may have to go with something like the following:

$(function(){

  $('.content .right').width($(window).width() - (480));
  $(window).bind("resize", function(){
    $('.content .right').width($(window).width() - (480));
  });

});
Jonathan Sampson
Great, thanks!!
Nimbuz
You're welcome.
Jonathan Sampson
A: 
function sizing() {
  $('.content .right').width($(window).width() - 480);
}

$(document).ready(sizing);
$(window).resize(sizing);
Emil Vikström