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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Tom BeckCommented:
$(window).on('load resize', function(e){
   if ($(this).width()<1024) { 
       //Do something;
  } 
});

Open in new window

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
Tom BeckCommented:
When I implement a function on resize however I usually introduce a delay before checking the new size and manipulating elements. That's because during a resize of the browser window your code will execute multiple times in rapid succession until the user is finished resizing. This can have some undesirable affects.

Use something like this to overcome the problem:
$(window).on('load resize', function(e){
	if (e.type == 'resize') {
		var contentW = $(this).width();
		var timer = setInterval(function(){ 
		   var newW = $(this).width();
			if (contentW == newW){
				clearInterval(timer);
				if (newW < 1024) { 
					 //DO SOMETHING;
				} 
			}
		}, 300); //check every 3ms to see if the window width has stopped changing
		         //set to longer interval if too many calls are being made to DO SOMETHING on resize
	} else {
		if (newW < 1024) { 
			 //DO SOMETHING;
		}
	}
});

Open in new window

NeverEndingFlashStoriesAuthor Commented:
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

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

NeverEndingFlashStoriesAuthor Commented:
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!
Tom BeckCommented:
Take out that document ready function wrapper.

Make sure you have a jQuery library v1.7 or newer.
NeverEndingFlashStoriesAuthor Commented:
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 :).
Tom BeckCommented:
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.
NeverEndingFlashStoriesAuthor Commented:
Gotcha. cheers :)
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.