Window Resize not firing Javascript/Jquery

Hi,

I have a jquery/javascript function that works great if you reload the page.  I would like to get this function in an OnReady function, but not sure if that is possible.  When reloading, after changing the screen dimension the function fires.  However, if you just do a resize of the window it does not.  I need it to fire everytime the window is resized.

    <script type="text/javascript">

        var eventFired = 0;
        if ($(window).width() < 960) {


            $(".topItems").hide();
            $(".siteBanner").hide();

        alert('Less than 960 - mobilized');

        } else {
            alert('More than 960 - demobilized');
            eventFired = 1;
        }

                    $(".topItems").hide();
                    $(".siteBanner").hide();

                    alert('Less than 960 resize - mobilized');
                } else {
                    alert('More than 960 resize');
                }
            }
        });

    </script>

Open in new window

atljarmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
$(window).resize(function() {

  // put all the code you want to run here

});

Open in new window

0
Julian HansenCommented:
There is something wrong with the code you have posted. You have a bunch of closing braces that don't have any opening braces - this code will never work as you have it.

Either you have not posted all your code or there are some serious problems in the code you do have. Based on what you have posted you should probably have something like this
$(function() {
    $(window).resize(function() {
        if ($(window).width() < 960) {
            $(".topItems").hide();
            $(".siteBanner").hide();
        }
     });
});

Open in new window

However without seeing more of your code we cannot say whether this is what you want.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
atljarmanAuthor Commented:
Thanks.  I can get this to work in the footer but not in the header of the document.  Any ideas?
0
Julian HansenCommented:
? What do you mean it works in the footer but not the header - do you mean where you place the script?

Look where you include your JQuery library - it has to come after that.
0
atljarmanAuthor Commented:
Thank you guys.  I got it working.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.