views:

1232

answers:

1

I need to get the body element height and width, when i resize the browser windows

Please help me to solve this problem using JQuery

+3  A: 

Use the resize event on the window object:

$(window).resize(function(){
  var width = $(document).width(), // or $(window).width()
      height = $(document).height(); // or $(window).height()
});
CMS
Hi CMS, When u try to alert those height and width it alerts the values atleast 4 times... why is that so??This happened in chrome browser
kayteen
This event might fire several times, while you are resizing the window
CMS
That's right. In IE especially, the resize event is triggered continuously as the window is being resized. Use a combination of setTimeout and clearTimeout with a short delay to prevent triggering multiple events
Chetan Sastry
Here's a nice implementation of what I mentioned above - http://www.matts411.com/post/delaying_javascript_event_execution/
Chetan Sastry