0

I'm trying to combine two of my codes which are identical, they only differ in the event handler. When the website is loaded it fires up my functions and if the user resizes it's browser it will update the variables and runs the function again.

$(window).load(function() {

        $(!agent) {

            var milk   = $().height(),
                choco  = $().width();

            doThis();

        } else {
            var orange  = $().length(),
                apple   = $().width();

            orThis();
        }

});

$(window).resize(function() {

        $(!agent) {

            var milk   = $().height(),
                choco  = $().width();

            doThis();

        } else {
            var orange  = $().length(),
                apple   = $().width();

            orThis();
        }

});
frenchie
  • 51,731
  • 109
  • 304
  • 510
Pennf0lio
  • 3,888
  • 8
  • 46
  • 70

2 Answers2

2

Or you can define the function:

function mySuperDuperEventHandler()
{
    $(!agent) {

        var milk   = $().height(),
            choco  = $().width();

        doThis();

    } else {
        var orange  = $().length(),
            apple   = $().width();

        orThis();
    }
}

And then assign it as the event handler:

$(window).on('load resize',mySuperDuperEventHandler);
jcaron
  • 17,302
  • 6
  • 32
  • 46
1

you can combine multiple events with the on() listener.

$(window).on('load resize',function() {

        $(!agent) {

            var milk   = $().height(),
                choco  = $().width();

            doThis();

        } else {
            var orange  = $().length(),
                apple   = $().width();

            orThis();
        }

});
r3wt
  • 4,642
  • 2
  • 33
  • 55