-2

Possible Duplicate:
JQuery: $(window).resize() doesn't work on Load

I have the below jQuery function. I want to use it twice. Once on window resize and again on page load without having to repeat myself. I have tried the below which works on resizing the browser window but the function is not working when I try to run again it on page load.

 window.onresize = function resize(event) {
    if(window.innerWidth < 620 ){
        $('#secondaryNav').hide();
        $('#search form').prependTo('div.content');
    } else{
        $('#secondaryNav').show();
        $('.content form').appendTo('#search');
    }

}

window.onload =  resize(event);

Any help would be greatly appreciated.

Community
  • 1
  • 1
Ollie
  • 646
  • 4
  • 13
  • 6
    You should have searched before posting: http://stackoverflow.com/questions/2597152/jquery-window-resize-doesnt-work-on-load – Sudhir Bastakoti Jun 13 '12 at 09:40
  • @Sudhir. I don't think it is a great post but it doesn't deserve so much downvotes. There're a lot worse question that even have positive votes. – gdoron Jun 13 '12 at 09:48
  • Note that older IE versions create two functions for named function expressions. – Felix Kling Jun 13 '12 at 09:52

1 Answers1

4
function resize(event) {
    if(window.innerWidth < 620 ){
        $('#secondaryNav').hide();
        $('#search form').prependTo('div.content');
    } else{
        $('#secondaryNav').show();
        $('.content form').appendTo('#search');
    }

}

window.onresize = resize:

window.onload = resize;
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164