Link to home
Start Free TrialLog in
Avatar of NeverEndingFlashStories
NeverEndingFlashStories

asked on

jquery on window resize and initial window value

Hi,
 
I am doing some jquery css modification for a responsive site , where the menu and other divs relocate themselves as the window is resized, or if the initial size of the window is that of a mobile equivalent.

I would like to do the equivalent of :

if (initial window size<1024 || window.resize) {
   Do something;
}  

However, the jquery function of $(window).resize(function(){ Do something }) does not seem to allow an if operator.
So far the only solution i can think of is to repeat the method twice, which seems silly. (see below)

 if ($(window).width()<1024) {
   Do something;
}  

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


Is there a more elegant solution to this?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NeverEndingFlashStories
NeverEndingFlashStories

ASKER

Hi Tom,

Thanks - this looks like it should work, and the tips on time checking is great.
Is there a requirement for me to run this? For some reason it isnt working for me :

             
$(document).ready(function(){
           $(window).on('load resize', function(e){
                if ($(this).width()<1024) {
                  console.log("hello world");
                }
            });
 });

Open in new window

Nevermind....nothing is working right now under document.ready, so..... something else outside the scope of my question is probably wrong.

Thanks for the help!
Take out that document ready function wrapper.

Make sure you have a jQuery library v1.7 or newer.
I figured it out...i was modifying the code of index.php, while refreshing about.php and wondering why nothing happens.

...its lunch time.

Thanks for the quick responses :).
Just noticed an error in my suggested delay code. In the "else" section, newW is out of scope so I would use $(this).width() instead.

Thanks for the points.
Gotcha. cheers :)